Java For Loop Practice
for loops: counting, summing, patterns, primes, series and string iteration.
1
Print 1 to N (For)
Easy
Read a positive integer N and print all integers from 1 to N, each on its own line, using a for 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
for (int i = 1; i
2
Print N to 1 (For)
Easy
Read a positive integer N and print N, N-1, ..., 1, each on its own line, using a for 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
for (int i = n; i >= 1; i--) print i;
3
Sum of N Natural Numbers (For)
Easy
Read a positive integer N and print 1 + 2 + ... + N using a for 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
for (int i = 1; i
4
Multiplication Table (For)
Easy
Read an integer N and print its multiplication table from 1 to 10 using a for 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
for (int i = 1; i
5
Sum of Even Numbers (For)
Easy
Read a positive integer N and print the sum of all even numbers from 1 to N using a for 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
for (int i = 2; i
1
Factorial (For)
Medium
Read a non-negative integer N and print N! using a for 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; for (int i = 2; i
2
Count Digits (For)
Medium
Read a positive integer N and print the number of digits. Convert N to a string and use a for loop over its characters.
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
String s = Integer.toString(n); for (int i = 0; i < s.length(); i++) count++;
3
Sum of Digits (For)
Medium
Read a positive integer N and print the sum of its digits. Use a for loop over the characters of its string form.
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
for (char c : Integer.toString(n).toCharArray()) sum += c - '0';
4
Armstrong Number (For)
Medium
Read a 3-digit integer N and print "Armstrong" if the sum of the cubes of its digits equals N, otherwise "Not Armstrong".
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
for each digit, cube it and add.
5
Prime Check (For)
Medium
Read an integer N > 1 and print "Prime" or "Not Prime". Use a for 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
for (int i = 2; i * i
6
Reverse a String (For)
Medium
Read a string and print it reversed using a for loop.
Input Format
A single line containing a string S (no spaces).
Output Format
The reversed string.
Sample Test Cases
|
Sample Input 1
hello |
Sample Output 1
olleh |
|
Sample Input 2
abc |
Sample Output 2
cba |
|
Sample Input 3
a |
Sample Output 3
a |
|
Sample Input 4
racecar |
Sample Output 4
racecar |
|
Sample Input 5
TechBridge24 |
Sample Output 5
42egdirBhceT |
Constraints
1
Explanation
for (int i = s.length()-1; i >= 0; i--) append s.charAt(i).
7
Print Odd Numbers
Medium
Read a positive integer N and print all odd numbers from 1 to N on one line, space-separated, using a for loop.
Input Format
A single integer N.
Output Format
Odds from 1 to N on one line.
Sample Test Cases
|
Sample Input 1
9 |
Sample Output 1
1 3 5 7 9 |
|
Sample Input 2
6 |
Sample Output 2
1 3 5 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
2 |
Sample Output 4
1 |
|
Sample Input 5
15 |
Sample Output 5
1 3 5 7 9 11 13 15 |
Constraints
1
Explanation
for (int i = 1; i
1
Print Prime Numbers in Range (For)
Hard
Read N and print all prime numbers from 2 to N, space-separated on one line. If there are none, print "None".
Input Format
A single integer N.
Output Format
Primes from 2 to N on one line, or "None".
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
2 3 5 7 |
|
Sample Input 2
20 |
Sample Output 2
2 3 5 7 11 13 17 19 |
|
Sample Input 3
2 |
Sample Output 3
2 |
|
Sample Input 4
50 |
Sample Output 4
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|
Sample Input 5
3 |
Sample Output 5
2 3 |
Constraints
2
Explanation
Nested for loops: for each number test divisibility.
2
Fibonacci Series (For)
Hard
Read N and print the first N Fibonacci numbers (0, 1, 1, 2, ...) on one line, space-separated, using a for 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
for (int i = 0; i < n; i++) { print a; next = a + b; a = b; b = next; }
3
Number Pattern (Right Triangle)
Hard
Read N and print N rows. Row i contains 1..i separated by spaces, using a for loop.
Input Format
A single integer N.
Output Format
N lines; line i has numbers 1..i.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
1 1 2 1 2 3 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
4 |
Sample Output 3
1 1 2 1 2 3 1 2 3 4 |
|
Sample Input 4
2 |
Sample Output 4
1 1 2 |
|
Sample Input 5
5 |
Sample Output 5
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 |
Constraints
1
Explanation
for (int i = 1; i
4
Power Using Loop
Hard
Read two integers B and E (non-negative) and print B^E using a for loop (no Math.pow).
Input Format
A single line with two integers B and E.
Output Format
A single integer: B^E.
Sample Test Cases
|
Sample Input 1
2 10 |
Sample Output 1
1024 |
|
Sample Input 2
5 3 |
Sample Output 2
125 |
|
Sample Input 3
0 0 |
Sample Output 3
1 |
|
Sample Input 4
0 5 |
Sample Output 4
0 |
|
Sample Input 5
10 10 |
Sample Output 5
10000000000 |
Constraints
0
Explanation
long r = 1; for (int i = 0; i < e; i++) r *= b;
5
Sum of Series
Hard
Read a positive integer N and print the sum 1 - 2 + 3 - 4 + ... ± N (alternating signs).
Input Format
A single integer N.
Output Format
A single integer: the alternating sum.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
3 |
|
Sample Input 2
4 |
Sample Output 2
-2 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
2 |
Sample Output 4
-1 |
|
Sample Input 5
10 |
Sample Output 5
-5 |
Constraints
1
Explanation
for (int i = 1; i
6
Largest Digit (For)
Hard
Read a positive integer N and print its largest digit. Use a for loop over the characters of its string form.
Input Format
A single integer N.
Output Format
A single integer: the largest digit.
Sample Test Cases
|
Sample Input 1
2913 |
Sample Output 1
9 |
|
Sample Input 2
100 |
Sample Output 2
1 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
987654321 |
Sample Output 4
9 |
|
Sample Input 5
50505 |
Sample Output 5
5 |
Constraints
1
Explanation
for (char c : Integer.toString(n).toCharArray()) track max of (c - '0').
7
Star Pattern (Triangle)
Hard
Read N and print N rows. Row i contains i asterisks (*), using nested for loops.
Input Format
A single integer N.
Output Format
N lines; line i has i asterisks.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
* ** *** |
|
Sample Input 2
1 |
Sample Output 2
* |
|
Sample Input 3
4 |
Sample Output 3
* ** *** **** |
|
Sample Input 4
2 |
Sample Output 4
* ** |
|
Sample Input 5
5 |
Sample Output 5
* ** *** **** ***** |
Constraints
1
Explanation
for (int i = 1; i
8
Count Divisors
Hard
Read a positive integer N and print the number of its positive divisors. Use a for loop.
Input Format
A single integer N.
Output Format
A single integer: the divisor count.
Sample Test Cases
|
Sample Input 1
12 |
Sample Output 1
6 |
|
Sample Input 2
7 |
Sample Output 2
2 |
|
Sample Input 3
1 |
Sample Output 3
1 |
|
Sample Input 4
100 |
Sample Output 4
9 |
|
Sample Input 5
16 |
Sample Output 5
5 |
Constraints
1
Explanation
for (int i = 1; i * i
Competitive MCQs — Java For Loop
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 38
Q1
What is the output of this code?
java
1
2
3
2
3
for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
}Correct!
Wrong — correct answer is .
i runs 0,1,2 then 3 fails the test.
Q2
What is the output of this code?
for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
}
for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
}
Correct!
Wrong — correct answer is .
i runs 0,1,2 then 3 fails the test.
Q3
Which part of a for loop is optional?
Correct!
Wrong — correct answer is .
for(;;) is valid; any or all parts may be empty.
Q4
What is the output of this code?
java
1
2
3
4
5
2
3
4
5
int s = 0;
for (int i = 1; i <= 4; i++) {
s += i;
}
System.out.println(s);Correct!
Wrong — correct answer is .
Sums 1+2+3+4 = 10.
Q5
What is the output of this code?
int s = 0;
for (int i = 1; i <= 4; i++) {
s += i;
}
System.out.println(s);
int s = 0;
for (int i = 1; i <= 4; i++) {
s += i;
}
System.out.println(s);
Correct!
Wrong — correct answer is .
Sums 1+2+3+4 = 10.
Q6
What is the output of this code?
java
1
2
3
2
3
for (int i = 10; i >= 8; i--) {
System.out.print(i + " ");
}Correct!
Wrong — correct answer is .
i counts down from 10 to 8.
Q7
What is the output of this code?
for (int i = 10; i >= 8; i--) {
System.out.print(i + " ");
}
for (int i = 10; i >= 8; i--) {
System.out.print(i + " ");
}
Correct!
Wrong — correct answer is .
i counts down from 10 to 8.
Q8
Which loop is best for iterating over all elements of an array?
Correct!
Wrong — correct answer is .
for (int v : arr) visits every element cleanly.
Q9
What is the output of this code?
java
1
2
3
4
5
2
3
4
5
int count = 0;
for (int i = 0; i < 10; i += 2) {
count++;
}
System.out.println(count);Correct!
Wrong — correct answer is .
i = 0,2,4,6,8 → five iterations.
Q10
What is the output of this code?
int count = 0;
for (int i = 0; i < 10; i += 2) {
count++;
}
System.out.println(count);
int count = 0;
for (int i = 0; i < 10; i += 2) {
count++;
}
System.out.println(count);
Correct!
Wrong — correct answer is .
i = 0,2,4,6,8 → five iterations.
Q11
What is the output of this code?
java
1
2
3
4
5
2
3
4
5
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.print(i * j + " ");
}
}Correct!
Wrong — correct answer is .
Nested loop prints i*j for i=1..3, j=1..2.
Q12
What is the output of this code?
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.print(i * j + " ");
}
}
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.print(i * j + " ");
}
}
Correct!
Wrong — correct answer is .
Nested loop prints i*j for i=1..3, j=1..2.
Q13
What is the output of this code?
java
1
2
3
4
2
3
4
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.print(i);
}Correct!
Wrong — correct answer is .
continue skips printing when i == 2.
Q14
What is the output of this code?
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.print(i);
}
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.print(i);
}
Correct!
Wrong — correct answer is .
continue skips printing when i == 2.
Q15
Which loop body never runs in this code?
java
1
2
3
2
3
for (int i = 5; i < 3; i++) {
System.out.println(i);
}Correct!
Wrong — correct answer is .
The condition 5 < 3 is false from the start.
Q16
Which loop body never runs in this code?
for (int i = 5; i < 3; i++) {
System.out.println(i);
}
for (int i = 5; i < 3; i++) {
System.out.println(i);
}
Correct!
Wrong — correct answer is .
The condition 5 < 3 is false from the start.
Q17
What is the output of this code?
java
1
2
3
4
5
2
3
4
5
int x = 1;
for (; x <= 3; ) {
System.out.print(x + " ");
x++;
}Correct!
Wrong — correct answer is .
Omitted init/update still work with an external counter.
Q18
What is the output of this code?
int x = 1;
for (; x <= 3; ) {
System.out.print(x + " ");
x++;
}
int x = 1;
for (; x <= 3; ) {
System.out.print(x + " ");
x++;
}
Correct!
Wrong — correct answer is .
Omitted init/update still work with an external counter.
Q19
What is the output of this code?
for (int i = 5; i >= 1; i--) {
System.out.print(i + " ");
}
for (int i = 5; i >= 1; i--) {
System.out.print(i + " ");
}
Correct!
Wrong — correct answer is .
The loop counts down from 5 to 1.
Q20
What is the output of this code?
int sum = 0;
for (int i = 1; i <= 10; i += 2) {
sum += i;
}
System.out.println(sum);
int sum = 0;
for (int i = 1; i <= 10; i += 2) {
sum += i;
}
System.out.println(sum);
Correct!
Wrong — correct answer is .
Odd numbers 1+3+5+7+9 = 25.
Q21
How many times does this loop print?
for (int i = 0; i < 4; i++) {
System.out.println(i);
}
for (int i = 0; i < 4; i++) {
System.out.println(i);
}
Correct!
Wrong — correct answer is .
i runs 0, 1, 2, 3 — four iterations.
Q22
What is the output of this code?
for (int i = 2; i <= 8; i *= 2) {
System.out.print(i + " ");
}
for (int i = 2; i <= 8; i *= 2) {
System.out.print(i + " ");
}
Correct!
Wrong — correct answer is .
i doubles 2→4→8, then 16 stops the loop.
Q23
What does the enhanced for loop iterate over?
Correct!
Wrong — correct answer is .
for (Type v : collection) visits every element.
Q24
What is the output of this code?
int[] a = {1, 2, 3};
for (int v : a) {
System.out.print(v * 2 + " ");
}
int[] a = {1, 2, 3};
for (int v : a) {
System.out.print(v * 2 + " ");
}
Correct!
Wrong — correct answer is .
Each element is doubled: 2, 4, 6.
Q25
What is the output of this code?
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
System.out.print(i + " ");
}
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
System.out.print(i + " ");
}
Correct!
Wrong — correct answer is .
continue skips even i, printing only odds 1 and 3.
Q26
What is the output of this code?
int count = 0;
for (int i = 1; i <= 100; i *= 10) {
count++;
}
System.out.println(count);
int count = 0;
for (int i = 1; i <= 100; i *= 10) {
count++;
}
System.out.println(count);
Correct!
Wrong — correct answer is .
i is 1, 10, 100 — three iterations.
Q27
Which loop is best for iterating an array by index?
Correct!
Wrong — correct answer is .
Index-based loops let you read and update specific positions.
Q28
What is the output of this code?
for (int i = 0, j = 10; i < 3; i++, j--) {
System.out.print((i + j) + " ");
}
for (int i = 0, j = 10; i < 3; i++, j--) {
System.out.print((i + j) + " ");
}
Correct!
Wrong — correct answer is .
Each step pairs i with j so i + j stays 10.
Q29
What is the output of this code?
for (int i = 3; i < 3; i++) {
System.out.println("hi");
}
for (int i = 3; i < 3; i++) {
System.out.println("hi");
}
Correct!
Wrong — correct answer is .
The condition 3 < 3 is false before the first iteration.
Q30
Which part of the for header runs only once?
Correct!
Wrong — correct answer is .
The init statement executes once before the loop begins.
Q31
What is the output of this code?
int x = 0;
for (int i = 0; i <= 4; i++) {
x += i;
}
System.out.println(x);
int x = 0;
for (int i = 0; i <= 4; i++) {
x += i;
}
System.out.println(x);
Correct!
Wrong — correct answer is .
The loop adds 0+1+2+3+4 = 10.
Q32
Which is TRUE about the for loop update clause?
Correct!
Wrong — correct answer is .
The update executes at the end of every pass.
Q33
What is the output of this code?
for (char c = 'a'; c <= 'c'; c++) {
System.out.print(c);
}
for (char c = 'a'; c <= 'c'; c++) {
System.out.print(c);
}
Correct!
Wrong — correct answer is .
Characters increment through a, b, c.
Q34
What is the output of this code?
for (int i = 1; i <= 5; i += 2) {
System.out.print(i + " ");
}
for (int i = 1; i <= 5; i += 2) {
System.out.print(i + " ");
}
Correct!
Wrong — correct answer is .
The step of 2 prints 1, 3, 5.
Q35
What happens when you use break in a for loop?
Correct!
Wrong — correct answer is .
break exits the loop and moves to the code after it.
Q36
What is the output of this code?
int total = 0;
for (int i = 1; i <= 4; i++) {
total += i * i;
}
System.out.println(total);
int total = 0;
for (int i = 1; i <= 4; i++) {
total += i * i;
}
System.out.println(total);
Correct!
Wrong — correct answer is .
Squares 1+4+9+16 = 30.
Q37
Which header loops from 1 to 10 inclusive?
Correct!
Wrong — correct answer is .
Starting at 1 and continuing while i
Q38
What is the output of this code?
for (int i = 0; i < 3; i++)
System.out.print(i);
System.out.print("!");
for (int i = 0; i < 3; i++)
System.out.print(i);
System.out.print("!");
Correct!
Wrong — correct answer is .
Without braces, only the print(i) is looped; "!" prints once after.