Starting from:
$30

$24

Flow Control Patterns Solution




control statements and loops




1. (Print a table) Write a program that displays the following table:

a b pow(a, b)

1 2 1

2 3 8

3 4 81

4 5 1024

5 6 15625







3. Write a program that generates two integers under 100

and prompts the user to enter the sum of these two integers. The program then

reports true if the answer is correct, false otherwise.







4. (Game: lottery) Revise Listing 3.9, Lottery.java, to generate a lottery of a threedigit

number. The program prompts the user to enter a three-digit number and

determines whether the user wins according to the following rules:

1. If the user input matches the lottery number in the exact order, the award is

$10,000.

2. If all the digits in the user input match all the digits in the lottery number, the

award is $3,000.

3. If one digit in the user input matches a digit in the lottery number, the award is

$1,000.










5. (Count positive and negative numbers and compute the average of numbers) Write

a program that reads an unspecified number of integers, determines how many

positive and negative values have been read, and computes the total and average of

the input values (not counting zeros). Your program ends with the input 0. Display

the average as a floating-point number. Here is a sample run:

Enter an integer, the input ends if it is 0:1 2 -1 3 0

The number of positives is 3

The number of negatives is 1

The total is 5

The average is 1.25




or




Enter an integer, the input ends if it is 0:

No numbers are entered except 0

0




6.(Display four patterns using loops) Use nested loops that display the following

patterns in four separate programs:

Pattern A Pattern B Pattern C Pattern D

1 1 2 3 4 5 6 1 1 2 3 4 5 6

1 2 1 2 3 4 5 2 1 1 2 3 4 5

1 2 3 1 2 3 4 3 2 1 1 2 3 4

1 2 3 4 1 2 3 4 3 2 1 1 2 3

1 2 3 4 5 1 2 5 4 3 2 1 1 2

1 2 3 4 5 6 1 6 5 4 3 2 1 1







7.(Occurrence of max numbers) Write a program that reads integers, finds the

largest of them, and counts its occurrences. Assume that the input ends with

number 0. Suppose that you entered 3 5 2 5 5 5 0; the program finds that the

largest is 5 and the occurrence count for 5 is 4.

(Hint: Maintain two variables, max and count. max stores the current max number,

and count stores its occurrences. Initially, assign the first number to max

and 1 to count. Compare each subsequent number with max. If the number is

greater than max, assign it to max and reset count to 1. If the number is equal to

max, increment count by 1.)

Enter numbers:

The largest number is 5

The occurrence count of the largest number is 4

3 5 2 5 5 5 0




8. (Decimal to hex) Write a program that prompts the user to enter an integer

between 0 and 15 and displays its corresponding hex number. Here are some sample

runs:

Enter a decimal value (0 to 15): 11

The hex value is B




Enter a decimal value (0 to 15): 5

The hex value is 5




Enter a decimal value (0 to 15): 288

Invalid input

More products