Professional Documents
Culture Documents
*
**
***
****
*****
*****
****
***
**
*
public class JavaPyramid2 {
System.out.print("*");
}
4;
*
**
***
****
*****
*****
****
***
**
*
}
}
*
**
***
****
*****
*****
****
***
**
*
12345
1234
123
12
1
System.out.println("");
}
}
}
Output of the example would be
12345
1234
123
12
1
6; This Java Pyramid example shows how to generate pyramid or triangle like
given below using for loop.
1
12
123
1234
12345
System.out.println("");
}
}
}
7; Write a program that inputs three integers from the user and displays the sum, average,
product, smallest and largest of the numbers.
8; Write a program that reads five integers and prints the largest and smallest integers in the
group.
9; write a program that prints A for exam grades greater than or equal to 90, B
for grades in the range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60
to 69 and F for all other grades:
10; For each of the following Java identifiers, note whether it is legal or illegal:
a. weeklySales
b. last character
c. class
d. public
e. myfirstinitial
f. phone#
g. abcdefghijklmnop
h. 23jordan
i. my_code
j. 90210
k. year2010budget
l. abffraternity
13; writes a program that reads and calculates the sum of an unspecified number
of integers. The input 0 signifies the end of the input (the input ends if it is 0)
By using
A; while loop
B; do while loop
14; what are the differences between a while loop and a do-while loop? Convert the
following while loop into a do-while loop.
Public class Converting {
Public static void main( string [] args){
Scanner input = new Scanner(System.in);
int sum = 0;
System.out.println("Enter an integer " +
"(the input ends if it is 0)");
int number = input.nextInt();
while (number != 0) {
sum += number;
System.out.println("Enter an integer " +"(the input ends if it is 0)");
number = input.nextInt();
}
}
}
15; Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, sum = 0, count;
for (count = 0; count < 5; count++) {
number = input.nextInt();
sum += number;
}
System.out.println("sum is " + sum);
System.out.println("count is " + count);
}
}
16; Which of the following statements are valid array declarations?
int i = new int(30);
double d[] = new double[30];
char[] r = new char(1..30);
int i[] = (3, 4, 3, 2);
float f[] = {2.3, 4.5, 6.6};
char[] c = new char();
17;
Indicate true or false for the following statements:
Every element in an array has the same type.
The array size is fixed after it is declared.
The array size is fixed after it is created.
The elements in an array must be of primitive data type.