You are on page 1of 7

Exercise Worksheet

Java Software Solutions

Loops (with solutions)
For exercises 1 to 15, indicate the output that will be produced. Assume the following declarations are made just before each exercise. That is, assume these initializations are in effect at the beginning of each problem:

final int MIN = 10, MAX = 20; int num = 15;

1.

while (num < MAX) { System.out.println (num); num = num + 1; } while (num < MAX) { num = num + 1; System.out.println (num); } do { num = num + 1; System.out.println (num); } while (num <= MAX);

15 16 17 18 19 16 17 18 19 20 16 17 18 19 20 21 15 14 13 infinitely…

2.

3.

4.

while (num < MAX) { System.out.println (num); num = num - 1; } while (num > MIN) { System.out.println (num); num = num - 1; }

5.

15 14 13 12 11

out.Exercise Worksheet Java Software Solutions 6. value++) System. } while (num < MAX) { if (num%2 == 0) System. num++. value < 0. 21 9.out. value >= 7. 15 17 19 7. num += 2. for (int value=7. 11. while (num < MAX) { System. value <= 20.println (value).println (value). 0 1 2 3 4 5 6 7 7 6 5 4 3 2 1 1 5 9 13 17 10. value+=4) System. value--) System. } do { num = num + 1.out. .println (value). if (num*2 > MAX+num) System.out.println (num).out.println (num). for (int value=0. 16 18 8.println (num). for (int value=1. } while (num <= MAX).out.

Exercise Worksheet Java Software Solutions 12. System. } ##### ##### ##### ##### ##### ##### ##### 15.println (value). count1 <= 7.out.print (count1*count2 + " ").println (value). value <= MAX. count2++) System.println(). value++) System. for (int count1=1. 15 16 17 18 19 20 15 17 18 19 13. } 1 2 3 4 5 2 4 6 8 10 3 4 5 6 8 10 9 12 15 12 16 20 15 20 25 . count2++) System. System. count1++) { for (int count2=1. count1++) { for (int count2=1.print ("#"). for (int value=num. value++) if (value%4 != 0) System. value <= MAX.out. for (int count1=1.out.out. count1 <= 5. count2 <= 5.out. for (int value=num. 14.out.println(). count2 <= 5.

print ("Enter another value:"). System.out. .out.readInt(). } 17. Read values from the user. System.out.out.readInt(). Assume the sentinel value is stored in a constant called SENTINEL.print ("Enter a positive value:"). value = Keyboard. System.readInt(). } 19. value = Keyboard. value = Keyboard. Read and print values entered by a user until a particular sentinel value is encountered.print ("Enter another value:").println ("Product: " + product).print ("Enter an even value:").print ("Enter a value:"). } while (value%2 != 0). (use a while loop) while (value <= 0) { System.out. } System.readInt(). product = 1.print ("Enter a value:"). System. Verify that the user enters a positive value.out. write code segments that will perform the specified action. while (value != 0) { product *= value.out. value = Keyboard. 16. value = Keyboard. Do not print the sentinel value. while (value != SENTINEL) { System. quitting when a sentinel value of 0 is entered. Compute and print the product of all values entered (excluding the sentinel value).out. 18.readInt().println (value). Verify that the user enters an even value (use a do loop) do { System.Exercise Worksheet Java Software Solutions For exercises 16 to 29.readInt(). value = Keyboard.

for (int count=2. count++) { System.Exercise Worksheet Java Software Solutions 20. . System. count <= number. Print the numbers between LOW and HIGH that are evenly divisible by four but not by five. 24. Print all of the factors of a value stored in the variable number. for (int num=1. 23. 22. 21.println (num). if (value > max) max = value. Assume the value is positive. count++) if (number%count == 0) System.out. for (int num=300. value = Keyboard.println (num). count <= 10.print ("Enter a value: "). num >= 3.out.out.print ("Enter another value: ").println ("Highest: " + max). num-=3) System.out. for (int count=LOW.out.out. if (value < min) min = value. min = max = value. } System.println (count). for (int count=1. Read 10 values from the user and print the lowest and highest value entered. count <= HIGH. System.readInt(). Print the multiples of 3 from 300 down to 3.out.out.readInt(). num <= 99.println ("Lowest: " + min). Print the odd numbers between 1 and 100. value = Keyboard. num+=2) System. count++) if (count%4 == 0 && count%5 != 0) System.println (count).

line++) { for (int space=0. for (int index=0. count = 0.length()-1. Print the characters stored in the String variable str backwards. space < line.Exercise Worksheet Java Software Solutions 25.out.out. index < str.print ("*"). System. continuing for LINES number of asterisks.length().length(). * * * * * * * for (int line=0.print (str. Print every other character in the String variable str starting with the first character. 28.print (" "). space++) System.out. Print a sequence of asterisk characters in the following configuration.out. } . index < str. index >= 0. 26.println ("Number of a's: " + count). Determine and print the number of times the character 'a' appears in the String variable str. for (int index=str.charAt(index) == 'a') count++.print (str.charAt(index)).out.charAt(index)). index--) System. System. index+=2) System. 27. line < LINES. index++) if (str. for (int index=0.

the output would be: C o m p i l e for (int line=0. space < line.charAt(line)). Print the characters of a String variable str in a diagonal line downward. if str contained "Compile". line < str. For example. } . System.print (str. space++) System.out.Exercise Worksheet Java Software Solutions 29.length().print (" "). line++) { for (int space=0.out.