Java Reference
In-Depth Information
11. Redo Exercise 9 using do ... while loops.
12. The program in the Fibonacci Number programming example does not check
whether the first number entered by the user is less than or equal to the second
number and both the numbers are non-negative. Also, the program does not
check whether the user entered a valid value for the position of the desired
number in the Fibonacci sequence. Rewrite the program so that it checks for
these conditions.
13. The population of a town A is less than the population of town B.
However, the population of town A is growing faster than the population
of town B. Write a program that prompts the user to enter the population
and growth rate of each town. The program outputs after how many years
the population of town A will be greater than or equal to the population of
town B and the populations of both the towns at that time. (A sample input
is: Population of town A ¼ 5,000, growth rate of town A ¼ 4%, population
of town B ¼ 8,000, and growth rate of town B ¼ 2%.)
Suppose that the first number of a sequence is x, where x is an integer.
Define a 0 ¼ x; a n+1 ¼ a n /2ifa n is even; a n+1 ¼ 3 a n +1ifa n is odd.
Then there exists an integer k such that a k ¼ 1. Write a program that
prompts the user to input the value of x. The program outputs the integer k
such that a k ¼ 1 and the numbers a 0 , a 1 , a 2 ,...,a k . (For example, if x ¼ 75,
then k ¼ 14, and the numbers a 0 , a 1 , a 2 ,...,a 14 , respectively, are 75, 226,
113, 340, 170, 85, 256, 128, 64, 32, 16, 8, 4, 2, 1.) Test your program for
the following values of x: 75, 111, 678, 732, 873, 2048, and 65535.
14.
15. Enhance your program of Exercise 14, by outputting the position of the
largest number and the largest number of the sequence a 0 , a 1 , a 2 , ..., a k .(For
example, the largest number of the sequence 75, 226, 113, 340, 170, 85, 256,
128, 64, 32, 16, 8, 4, 2, 1 is 340, and its position is 4.) Test your program for
the following values of x: 75, 111, 678, 732, 873, 2048, and 65535.
16. The program in Example 5-6 implements the Guessing the Number game.
However, in that program, the user is given as many tries as needed to guess
the correct number. Rewrite that program so that the user has, at most, five
tries to guess the correct number. Your program should print an appropriate
message, such as ''You win!'' or ''You lose!''
17. Example 5-6 implements the Guessing the Number game program. If the
guessed number is not correct, the program outputs a message indicating
whether the guess is low or high. Modify the program as follows: Suppose
that the variables num and guess are as declared in Example 5-6 and diff is
an int variable. Let diff = the absolute value of (num - guess) .If diff
is 0 , then guess is correct and the program outputs a message indicating
that the user guessed the correct number. Suppose diff is not 0 . Then, the
program outputs the message as follows:
Search WWH ::




Custom Search