while loops answer key
while loops answer key
Track the following code segments to determine what they will print.
7. int num = 0; 0 0
while (num < 3) 1 1
{ 2 2
System.out.println(num); 3 stop
num++;
}
12. Write a “while” loop that prints the integers from 1 through 10 on the same line.
int num = 1;
while (num < 11) // or (num <= 10)
{
System.out.print(num)
num++; }
13. Write a “while” loop that prints the integers 10 down to 1 on the same line.
14. Write a “while” loop that prints the even integers 2 through 24 on separate lines.
int num = 2;
while (num < 26)
{
System.out.println(num)
num += 2; } // or num = num + 2
15. Write a “while” loop that prints multiples of 3 between 9 and 27 on the same line.
int num = 9;
while (num < 30) // or (num <= 27)
{
System.out.print(num)
num += 3; } // or num = num + 3
16. Write a “while” loop that prints numbers that are multiples of either 3 or 7 between 3 and 51.
int num = 3;
while (num <= 51)
{
if (num % 3 == 0 || num % 7 == 0)
System.out.println(num)
num++; }