- Trace the execution of the following loop by showing the value of
each variable at each iteration in the table next to the code.
final int LIMIT = 15; num sum
int num = 1; --- ---
int sum = 0;
while (num < LIMIT)
{
num = num + 3;
sum = sum + num;
}
System.out.println ("Last number is " + num + " and sum is " + sum);
- What is printed by the above code?
- Write a for loop equivalent to the while loop above.
- Complete the following code to print the string read in backwards.
Use a for loop.
String str;
int length;
System.out.print ("Enter a string of characters: ");
str = scan.nextLine();
// Find the length of str
________________________________________________________________;
// Write a for loop to print the string backwards
- The following nested loop prints a triangle of stars (see page 250 of
your text - it shows the whole program and the output).
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star++)
System.out.print ("*");
System.out.println();
}
Modify the code to print the "upside down" triangle in part (a) of
Programming Project 5.10 on page 280 of the textbook.
- Write a fragment of code that reads in a sequence of numbers
and finds the smallest number read in. Use a do ... while loop.
Control the loop by asking the user if there are more numbers to
enter. Read the answer into a String variable. You may assume the
numbers will be in the range -1000 to +1000.