COMP248 Tutorial 06
COMP248 Tutorial 06
Question 1:
What is the output of the following?
A.
int count = 0;
while (count <= 6)
{
System.out.print(count + " ");
count = count + 2;
}
System.out.println( );
B.
int count = 7;
while (count >= 4)
{
System.out.print(count + " ");
count = count - 1;
}
System.out.println( );
C.
int i; int j;
boolean again = true;
1
D.
int a = 30;
int b = 3;
while (a >= b)
{
System.out.println("while " + a + " " + b);
if ((a % b) == 0)
{
a = a / b;
b++;
}
else
{
a = a - 1;
b = b - 1;
}
}
System.out.println("the end " + a + " " + b);
E.
int i = 5, count = 0;
while (i != 1)
{
System.out.println(count + " " + i);
count++;
if ((i % 2) == 0)
i /= 2;
else
i = 3 * i + 1;
}
2
F.
boolean sign = true;
int sum = 0;
int n = 0;
G.
int x = 0;
while (x != 8);
{
System.out.print("Hello");
x = x + 1;
}
H.
int x = 0;
while (x != 8);
{
System.out,print("Hello");
}
Question 2:
Write Java code that uses a do…while loop that prints even numbers from 2
through 10.
3
Question 3:
Write Java code that uses a while loop to print even numbers from 2 through 10.
Question 4:
Write Java code that uses a do while loop to sum the numbers from 1 through
50. Display the total sum to the console.
Question 5:
Write a Java program using a while loop to find and display the smallest positive
integer whose remainder:
• when divided by 3 is 1,
• when divided by 5 is 2, and
• when divided by 7 is 3.