Java Reference
In-Depth Information
4.6
( Conversion from miles to kilometers ) Write a program that displays the following
two tables side by side (note that 1 mile is 1.609 kilometers and that 1 kilometer is
.621 miles):
Miles Kilometers | Kilometers Miles
1 1.609 | 20 12.430
2 3.218 | 25 15.538
...
9 14.481 | 60 37.290
10 16.090 | 65 40.398
**4.7
( Financial application: compute future tuition ) Suppose that the tuition for a uni-
versity is $10,000 this year and increases 5% every year. Write a program that
computes the tuition in ten years and the total cost of four years' worth of tuition
starting ten years from now.
4.8
( Find the highest score ) Write a program that prompts the user to enter the number
of students and each student's name and score, and finally displays the name of the
student with the highest score.
*4.9
( Find the two highest scores ) Write a program that prompts the user to enter
the number of students and each student's name and score, and finally displays the
student with the highest score and the student with the second-highest score.
4.10
( Find numbers divisible by 5 and 6 ) Write a program that displays all the numbers
from 100 to 1,000, ten per line, that are divisible by 5 and 6. Numbers are sepa-
rated by exactly one space.
4.11
( Find numbers divisible by 5 or 6, but not both ) Write a program that displays all
the numbers from 100 to 200, ten per line, that are divisible by 5 or 6, but not both.
Numbers are separated by exactly one space.
4.12
( Find the smallest n such that n 2 12,000 ) Use a while loop to find the smallest
integer n such that n 2 is greater than 12,000.
7
4.13
( Find the largest n such that n 3 12,000 ) Use a while loop to find the largest
integer n such that n 3 is less than 12,000.
6
Sections 4.8-4.10
*4.14
( Compute the greatest common divisor ) Another solution for Listing 4.9 to find
the greatest common divisor of two integers n1 and n2 is as follows: First find d to
be the minimum of n1 and n2 , then check whether d , d-1 , d-2 , ..., 2 , or 1 is a
divisor for both n1 and n2 in this order. The first such common divisor is the great-
est common divisor for n1 and n2 . Write a program that prompts the user to enter
two positive integers and displays the gcd.
*4.15
( Display the ASCII character table ) Write a program that prints the characters in
the ASCII character table from ! to ~ . Display ten characters per line. The ASCII
table is shown in Appendix B. Characters are separated by exactly one space.
*4.16
( Find the factors of an integer ) Write a program that reads an integer and displays
all its smallest factors in increasing order. For example, if the input integer is 120 ,
the output should be as follows: 2 , 2 , 2 , 3 , 5 .
**4.17
( Display pyramid ) Write a program that prompts the user to enter an integer from
1 to 15 and displays a pyramid, as shown in the following sample run:
Search WWH ::




Custom Search