Java Reference
In-Depth Information
d)
The following code should output the even integers from 2 to 100:
counter = 2 ;
do
{
System.out.println(counter);
counter += 2 ;
} While (counter < 100 );
5.10
What does the following program do?
1
// Exercise 5.10: Printing.java
2
public class Printing
3
{
4
public static void main(String[] args)
5
{
6
for ( int i = 1 ; i <= 10 ; i++)
7
{
8
for ( int j = 1 ; j <= 5 ; j++)
9
System.out.print( '@' );
10
11
System.out.println();
12
}
13
}
14
} // end class Printing
5.11 (Find the Smallest Value) Write an application that finds the smallest of several integers.
Assume that the first value read specifies the number of values to input from the user.
5.12 (Calculating the Product of Odd Integers) Write an application that calculates the product
of the odd integers from 1 to 15.
5.13 (Factorials) Factorials are used frequently in probability problems. The factorial of a positive
integer n (written n ! and pronounced “ n factorial”) is equal to the product of the positive integers from
1 to n . Write an application that calculates the factorials of 1 through 20. Use type long . Display the
results in tabular format. What difficulty might prevent you from calculating the factorial of 100?
5.14 (Modified Compound-Interest Program) Modify the compound-interest application of
Fig. 5.6 to repeat its steps for interest rates of 5%, 6%, 7%, 8%, 9% and 10%. Use a for loop to
vary the interest rate.
5.15 (Triangle Printing Program) Write an application that displays the following patterns sep-
arately, one below the other. Use for loops to generate the patterns. All asterisks ( * ) should be print-
ed by a single statement of the form System.out.print('*' ) ; which causes the asterisks to print side
by side. A statement of the form System.out.println(); can be used to move to the next line. A
statement of the form System.out.print(' ' ) ; can be used to display a space for the last two pat-
terns. There should be no other output statements in the program. [ Hint: The last two patterns re-
quire that each line begin with an appropriate number of blank spaces.]
(a)
(b)
(c)
(d)
*
**********
**********
*
**
*********
*********
**
***
********
********
***
****
*******
*******
****
*****
******
******
*****
******
*****
*****
******
*******
****
****
*******
********
***
***
********
*********
**
**
*********
**********
*
*
**********
 
Search WWH ::




Custom Search