Java While Loop Practice
while and do-while loops: digit tricks, GCD, Fibonacci, patterns and more.
1
Print 1 to N (While)
Easy
Read a positive integer N and print all integers from 1 to N, each on its own line, using a while loop.
Input Format
A single integer N.
Output Format
N lines: 1, 2, ..., N.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
1 2 3 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
5 |
Sample Output 3
1 2 3 4 5 |
|
Sample Input 4
2 |
Sample Output 4
1 2 |
|
Sample Input 5
10 |
Sample Output 5
1 2 3 4 5 6 7 8 9 10 |
Constraints
1
Explanation
int i = 1; while (i
2
Print N to 1 (While)
Easy
Read a positive integer N and print N, N-1, ..., 1, each on its own line, using a while loop.
Input Format
A single integer N.
Output Format
N lines: N, N-1, ..., 1.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
3 2 1 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
5 |
Sample Output 3
5 4 3 2 1 |
|
Sample Input 4
2 |
Sample Output 4
2 1 |
|
Sample Input 5
7 |
Sample Output 5
7 6 5 4 3 2 1 |
Constraints
1
Explanation
int i = n; while (i >= 1) { print i; i--; }
3
Sum of N Natural Numbers (While)
Easy
Read a positive integer N and print 1 + 2 + ... + N using a while loop.
Input Format
A single integer N.
Output Format
A single integer: the sum.
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
55 |
|
Sample Input 2
5 |
Sample Output 2
15 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
100 |
Sample Output 4
5050 |
|
Sample Input 5
3 |
Sample Output 5
6 |
Constraints
1
Explanation
int s = 0, i = 1; while (i
4
Count Digits (While)
Easy
Read a positive integer N and print the number of digits using a while loop.
Input Format
A single integer N.
Output Format
A single integer: the digit count.
Sample Test Cases
|
Sample Input 1
12345 |
Sample Output 1
5 |
|
Sample Input 2
7 |
Sample Output 2
1 |
|
Sample Input 3
0 |
Sample Output 3
1 |
|
Sample Input 4
1000000000 |
Sample Output 4
10 |
|
Sample Input 5
999 |
Sample Output 5
3 |
Constraints
1
Explanation
while (n > 0) { count++; n /= 10; }
5
Sum of Digits (While)
Easy
Read a positive integer N and print the sum of its digits using a while loop.
Input Format
A single integer N.
Output Format
A single integer: the digit sum.
Sample Test Cases
|
Sample Input 1
1234 |
Sample Output 1
10 |
|
Sample Input 2
999 |
Sample Output 2
27 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
1000000000 |
Sample Output 4
1 |
|
Sample Input 5
987654321 |
Sample Output 5
45 |
Constraints
1
Explanation
while (n > 0) { sum += n % 10; n /= 10; }
6
Multiplication Table (While)
Easy
Read an integer N and print its multiplication table from 1 to 10 using a while loop, one per line, in the format "N x i = product".
Input Format
A single integer N.
Output Format
10 lines of the form "N x i = product".
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50 |
|
Sample Input 2
2 |
Sample Output 2
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 |
|
Sample Input 3
3 |
Sample Output 3
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 |
Constraints
1
Explanation
int i = 1; while (i
1
Reverse Digits (While)
Medium
Read a positive integer N and print it with digits reversed (leading zeros dropped) using a while loop.
Input Format
A single integer N.
Output Format
A single integer: the reversed number.
Sample Test Cases
|
Sample Input 1
12345 |
Sample Output 1
54321 |
|
Sample Input 2
100 |
Sample Output 2
1 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
9009 |
Sample Output 4
9009 |
|
Sample Input 5
123456789 |
Sample Output 5
987654321 |
Constraints
1
Explanation
while (n > 0) { rev = rev * 10 + n % 10; n /= 10; }
2
Factorial (While)
Medium
Read a non-negative integer N and print N! using a while loop.
Input Format
A single integer N.
Output Format
A single integer: N!.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
120 |
|
Sample Input 2
0 |
Sample Output 2
1 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
10 |
Sample Output 4
3628800 |
|
Sample Input 5
20 |
Sample Output 5
2432902008176640000 |
Constraints
0
Explanation
long f = 1; while (n > 1) { f *= n; n--; }
3
Sum of Even Numbers (While)
Medium
Read a positive integer N and print the sum of all even numbers from 1 to N using a while loop.
Input Format
A single integer N.
Output Format
A single integer: the sum of evens.
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
30 |
|
Sample Input 2
5 |
Sample Output 2
6 |
|
Sample Input 3
1 |
Sample Output 3
0 |
|
Sample Input 4
2 |
Sample Output 4
2 |
|
Sample Input 5
100 |
Sample Output 5
2550 |
Constraints
1
Explanation
int i = 2; while (i
4
Armstrong Number (3-digit)
Medium
Read a 3-digit integer N and print "Armstrong" if the sum of the cubes of its digits equals N, otherwise "Not Armstrong". Use a while loop to extract digits.
Input Format
A single integer N (100-999).
Output Format
"Armstrong" or "Not Armstrong".
Sample Test Cases
|
Sample Input 1
153 |
Sample Output 1
Armstrong |
|
Sample Input 2
123 |
Sample Output 2
Not Armstrong |
|
Sample Input 3
370 |
Sample Output 3
Armstrong |
|
Sample Input 4
371 |
Sample Output 4
Armstrong |
|
Sample Input 5
100 |
Sample Output 5
Not Armstrong |
Constraints
100
Explanation
Extract each digit with % 10 and / 10 in a while loop.
5
Prime Check (While)
Medium
Read an integer N > 1 and print "Prime" or "Not Prime". Use a while loop to test divisors.
Input Format
A single integer N.
Output Format
"Prime" or "Not Prime".
Sample Test Cases
|
Sample Input 1
17 |
Sample Output 1
Prime |
|
Sample Input 2
15 |
Sample Output 2
Not Prime |
|
Sample Input 3
2 |
Sample Output 3
Prime |
|
Sample Input 4
97 |
Sample Output 4
Prime |
|
Sample Input 5
100 |
Sample Output 5
Not Prime |
Constraints
2
Explanation
Check divisors 2..sqrt(N) with a while loop.
6
Do-While: Print 1 to N
Medium
Read a positive integer N and print 1 to N, each on its own line, using a do-while loop (so it runs at least once).
Input Format
A single integer N.
Output Format
N lines: 1, 2, ..., N.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
1 2 3 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
4 |
Sample Output 3
1 2 3 4 |
|
Sample Input 4
2 |
Sample Output 4
1 2 |
|
Sample Input 5
6 |
Sample Output 5
1 2 3 4 5 6 |
Constraints
1
Explanation
int i = 1; do { print i; i++; } while (i
7
First N Multiples
Medium
Read two integers N and K and print the first N multiples of K on one line, space-separated, using a while loop.
Input Format
A single line with two integers N and K.
Output Format
The first N multiples of K on one line.
Sample Test Cases
|
Sample Input 1
5 3 |
Sample Output 1
3 6 9 12 15 |
|
Sample Input 2
3 7 |
Sample Output 2
7 14 21 |
|
Sample Input 3
1 10 |
Sample Output 3
10 |
|
Sample Input 4
4 2 |
Sample Output 4
2 4 6 8 |
|
Sample Input 5
6 5 |
Sample Output 5
5 10 15 20 25 30 |
Constraints
1
Explanation
int i = 1; while (i
8
Print Even Numbers (While)
Medium
Read a positive integer N and print all even numbers from 2 to N on one line, space-separated, using a while loop.
Input Format
A single integer N.
Output Format
Evens from 2 to N on one line.
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
2 4 6 8 10 |
|
Sample Input 2
7 |
Sample Output 2
2 4 6 |
|
Sample Input 3
1 |
Sample Output 3
|
|
Sample Input 4
2 |
Sample Output 4
2 |
|
Sample Input 5
20 |
Sample Output 5
2 4 6 8 10 12 14 16 18 20 |
Constraints
1
Explanation
int i = 2; while (i
1
GCD of Two Numbers (While)
Hard
Read two positive integers A and B and print their GCD using the Euclidean algorithm in a while loop.
Input Format
A single line with two integers A and B.
Output Format
A single integer: gcd(A, B).
Sample Test Cases
|
Sample Input 1
48 18 |
Sample Output 1
6 |
|
Sample Input 2
12 8 |
Sample Output 2
4 |
|
Sample Input 3
17 5 |
Sample Output 3
1 |
|
Sample Input 4
100 75 |
Sample Output 4
25 |
|
Sample Input 5
1000000 500000 |
Sample Output 5
500000 |
Constraints
1
Explanation
while (b != 0) { int t = b; b = a % b; a = t; } print a.
2
Fibonacci Series (While)
Hard
Read N and print the first N Fibonacci numbers (0, 1, 1, 2, ...) on one line, space-separated, using a while loop.
Input Format
A single integer N.
Output Format
The first N Fibonacci numbers on one line.
Sample Test Cases
|
Sample Input 1
7 |
Sample Output 1
0 1 1 2 3 5 8 |
|
Sample Input 2
3 |
Sample Output 2
0 1 1 |
|
Sample Input 3
1 |
Sample Output 3
0 |
|
Sample Input 4
10 |
Sample Output 4
0 1 1 2 3 5 8 13 21 34 |
|
Sample Input 5
5 |
Sample Output 5
0 1 1 2 3 |
Constraints
1
Explanation
Track a and b; print a, then update both.
3
Number Pattern (While)
Hard
Read N and print N rows. Row i (1-based) contains 1..(N-i+1) separated by spaces, using while loops.
Input Format
A single integer N.
Output Format
N lines; line i has numbers 1..(N-i+1).
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
1 2 3 1 2 1 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
4 |
Sample Output 3
1 2 3 4 1 2 3 1 2 1 |
|
Sample Input 4
2 |
Sample Output 4
1 2 1 |
|
Sample Input 5
5 |
Sample Output 5
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 |
Constraints
1
Explanation
Outer while for rows, inner while for columns.
4
Sum of Digits Until Single
Hard
Read a positive integer N and keep summing its digits until a single digit remains. Print that digit. Use a while loop.
Input Format
A single integer N.
Output Format
A single integer: the digital root.
Sample Test Cases
|
Sample Input 1
12345 |
Sample Output 1
6 |
|
Sample Input 2
999 |
Sample Output 2
9 |
|
Sample Input 3
7 |
Sample Output 3
7 |
|
Sample Input 4
100 |
Sample Output 4
1 |
|
Sample Input 5
987654321 |
Sample Output 5
9 |
Constraints
1
Explanation
while (n >= 10) { sum digits of n; n = sum; } print n.
5
Prime Factors
Hard
Read a positive integer N (> 1) and print all its prime factors in increasing order, each on its own line, with repetition (e.g. 12 → 2, 2, 3). Use a while loop.
Input Format
A single integer N.
Output Format
One prime factor per line, in increasing order.
Sample Test Cases
|
Sample Input 1
12 |
Sample Output 1
2 2 3 |
|
Sample Input 2
18 |
Sample Output 2
2 3 3 |
|
Sample Input 3
7 |
Sample Output 3
7 |
|
Sample Input 4
100 |
Sample Output 4
2 2 5 5 |
|
Sample Input 5
30 |
Sample Output 5
2 3 5 |
Constraints
2
Explanation
Divide out 2s, then odd factors up to sqrt(N).
6
Reverse Number Pattern
Hard
Read N and print N rows. Row i contains the numbers N-i+1 down to 1, space-separated, using while loops.
Input Format
A single integer N.
Output Format
N lines; line i has numbers (N-i+1)..1.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
3 2 1 2 1 1 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
4 |
Sample Output 3
4 3 2 1 3 2 1 2 1 1 |
|
Sample Input 4
2 |
Sample Output 4
2 1 1 |
|
Sample Input 5
5 |
Sample Output 5
5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 |
Constraints
1
Explanation
Outer while for rows, inner while printing descending.
Competitive MCQs — Java While Loop
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 37
Q1
What is the output of this code?
java
1
2
3
4
5
2
3
4
5
int i = 1;
while (i <= 3) {
System.out.print(i + " ");
i++;
}Correct!
Wrong — correct answer is .
i goes 1,2,3 then stops at 4.
Q2
What is the output of this code?
int i = 1;
while (i <= 3) {
System.out.print(i + " ");
i++;
}
int i = 1;
while (i <= 3) {
System.out.print(i + " ");
i++;
}
Correct!
Wrong — correct answer is .
i goes 1,2,3 then stops at 4.
Q3
Which loop checks its condition before executing the body?
Correct!
Wrong — correct answer is .
while tests first; do-while runs the body at least once.
Q4
What is the output of this code?
java
1
2
3
4
2
3
4
int i = 5;
while (i > 0) {
System.out.print(i-- + " ");
}Correct!
Wrong — correct answer is .
i-- prints the current value then decrements: 5..1.
Q5
What is the output of this code?
int i = 5;
while (i > 0) {
System.out.print(i-- + " ");
}
int i = 5;
while (i > 0) {
System.out.print(i-- + " ");
}
Correct!
Wrong — correct answer is .
i-- prints the current value then decrements: 5..1.
Q6
Which loop body always executes at least once?
Correct!
Wrong — correct answer is .
do-while checks after the body, guaranteeing one run.
Q7
What is the output of this code?
java
1
2
3
4
5
6
2
3
4
5
6
int sum = 0, i = 1;
while (i <= 5) {
sum += i;
i++;
}
System.out.println(sum);Correct!
Wrong — correct answer is .
This sums 1+2+3+4+5 = 15.
Q8
What is the output of this code?
int sum = 0, i = 1;
while (i <= 5) {
sum += i;
i++;
}
System.out.println(sum);
int sum = 0, i = 1;
while (i <= 5) {
sum += i;
i++;
}
System.out.println(sum);
Correct!
Wrong — correct answer is .
This sums 1+2+3+4+5 = 15.
Q9
Which statement immediately ends a while loop?
Correct!
Wrong — correct answer is .
break terminates the loop; continue skips to the next iteration.
Q10
What is the output of this code?
java
1
2
3
4
5
6
2
3
4
5
6
int x = 10;
while (x > 5) {
if (x == 8) break;
System.out.print(x + " ");
x--;
}Correct!
Wrong — correct answer is .
Prints 10, 9, then x==8 triggers break.
Q11
What is the output of this code?
int x = 10;
while (x > 5) {
if (x == 8) break;
System.out.print(x + " ");
x--;
}
int x = 10;
while (x > 5) {
if (x == 8) break;
System.out.print(x + " ");
x--;
}
Correct!
Wrong — correct answer is .
Prints 10, 9, then x==8 triggers break.
Q12
What is the output of this code?
java
1
2
3
4
5
6
2
3
4
5
6
int i = 0;
while (i < 5) {
i++;
if (i % 2 == 0) continue;
System.out.print(i + " ");
}Correct!
Wrong — correct answer is .
Odd i values (1,3,5) print; even ones continue.
Q13
What is the output of this code?
int i = 0;
while (i < 5) {
i++;
if (i % 2 == 0) continue;
System.out.print(i + " ");
}
int i = 0;
while (i < 5) {
i++;
if (i % 2 == 0) continue;
System.out.print(i + " ");
}
Correct!
Wrong — correct answer is .
Odd i values (1,3,5) print; even ones continue.
Q14
How many times does this loop run?
java
1
2
3
2
3
int i = 3;
while (i > 0)
i--;Correct!
Wrong — correct answer is .
Body runs while i is 3, 2, 1 — three times.
Q15
How many times does this loop run?
int i = 3;
while (i > 0)
i--;
int i = 3;
while (i > 0)
i--;
Correct!
Wrong — correct answer is .
Body runs while i is 3, 2, 1 — three times.
Q16
What is the output of this code?
java
1
2
3
4
5
2
3
4
5
int i = 1;
do {
System.out.print(i + " ");
i++;
} while (i < 3);Correct!
Wrong — correct answer is .
Body prints 1 and 2; stops when i reaches 3.
Q17
What is the output of this code?
int i = 1;
do {
System.out.print(i + " ");
i++;
} while (i < 3);
int i = 1;
do {
System.out.print(i + " ");
i++;
} while (i < 3);
Correct!
Wrong — correct answer is .
Body prints 1 and 2; stops when i reaches 3.
Q18
Which loop checks its condition AFTER the body runs?
Correct!
Wrong — correct answer is .
do-while guarantees at least one execution before testing.
Q19
What is the output of this code?
int i = 0;
while (i < 3) {
System.out.print(i + " ");
i++;
}
int i = 0;
while (i < 3) {
System.out.print(i + " ");
i++;
}
Correct!
Wrong — correct answer is .
i prints 0, 1, 2 and stops when i reaches 3.
Q20
What is an infinite loop?
Correct!
Wrong — correct answer is .
If the exit condition never fails, the loop runs forever.
Q21
What is the output of this code?
int x = 1;
while (x < 100) {
x *= 2;
}
System.out.println(x);
int x = 1;
while (x < 100) {
x *= 2;
}
System.out.println(x);
Correct!
Wrong — correct answer is .
x doubles to 2,4,8,16,32,64,128 — stopping when it reaches 128.
Q22
What is the output of this code?
int i = 5;
while (i >= 1) {
System.out.print(i-- + " ");
}
int i = 5;
while (i >= 1) {
System.out.print(i-- + " ");
}
Correct!
Wrong — correct answer is .
i-- prints the value then decrements, ending after 1.
Q23
Which statement skips the rest of the current iteration?
Correct!
Wrong — correct answer is .
continue jumps to the loop condition for the next iteration.
Q24
What is the output of this code?
int i = 0;
while (i < 5) {
i++;
if (i == 3) continue;
System.out.print(i + " ");
}
int i = 0;
while (i < 5) {
i++;
if (i == 3) continue;
System.out.print(i + " ");
}
Correct!
Wrong — correct answer is .
i=3 is skipped by continue; the others print.
Q25
What is the output of this code?
int i = 0;
while (i < 10) {
if (i == 4) break;
i++;
}
System.out.println(i);
int i = 0;
while (i < 10) {
if (i == 4) break;
i++;
}
System.out.println(i);
Correct!
Wrong — correct answer is .
break exits when i equals 4, before the increment.
Q26
How many times does this run?
int i = 3;
while (i > 0) {
i--;
}
int i = 3;
while (i > 0) {
i--;
}
Correct!
Wrong — correct answer is .
The body runs while i is 3, 2, 1 — three times.
Q27
What is the output of this code?
int count = 0;
int i = 1;
while (i <= 4) {
count += i;
i++;
}
System.out.println(count);
int count = 0;
int i = 1;
while (i <= 4) {
count += i;
i++;
}
System.out.println(count);
Correct!
Wrong — correct answer is .
The loop sums 1+2+3+4 = 10.
Q28
Which loop body never executes?
Correct!
Wrong — correct answer is .
A while with a false condition skips the body entirely.
Q29
What is the output of this code?
do {
System.out.print("once ");
} while (false);
do {
System.out.print("once ");
} while (false);
Correct!
Wrong — correct answer is .
do-while runs the body once before testing the false condition.
Q30
What is the output of this code?
int i = 10;
while (i > 0) {
i -= 3;
}
System.out.println(i);
int i = 10;
while (i > 0) {
i -= 3;
}
System.out.println(i);
Correct!
Wrong — correct answer is .
i goes 10, 7, 4, 1, then -2 which fails i > 0.
Q31
Which is TRUE about while and do-while?
Correct!
Wrong — correct answer is .
The placement of the condition test is the key difference.
Q32
What is the output of this code?
int i = 2;
while (i <= 6) {
System.out.print(i + " ");
i += 2;
}
int i = 2;
while (i <= 6) {
System.out.print(i + " ");
i += 2;
}
Correct!
Wrong — correct answer is .
i steps 2, 4, 6 and stops after 6.
Q33
What happens if the loop condition never changes?
Correct!
Wrong — correct answer is .
A constant true condition with no exit path loops forever.
Q34
What is the output of this code?
int i = 1;
while (i < 5)
i *= 2;
System.out.println(i);
int i = 1;
while (i < 5)
i *= 2;
System.out.println(i);
Correct!
Wrong — correct answer is .
i doubles 1→2→4→8, and 8 is not less than 5.
Q35
Which keyword unconditionally exits a loop?
Correct!
Wrong — correct answer is .
break terminates the loop immediately.
Q36
What is the output of this code?
int i = 0;
while (i < 3) {
i++;
}
System.out.println(i);
int i = 0;
while (i < 3) {
i++;
}
System.out.println(i);
Correct!
Wrong — correct answer is .
The loop increments i to 1, 2, 3 and stops.
Q37
How would you count down from 5 to 1 with a while loop?
Correct!
Wrong — correct answer is .
Starting at 5 and decrementing to 1 prints the descending sequence.