0
0
Cprogramming~20 mins

For loop in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a nested for loop
What is the output of this C code snippet?
C
#include <stdio.h>

int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 2; j++) {
            printf("%d%d ", i, j);
        }
    }
    return 0;
}
AError: Syntax error
B12 11 22 21 32 31
C11 21 31 12 22 32
D11 12 21 22 31 32
Attempts:
2 left
💡 Hint
Think about how the inner loop runs completely for each iteration of the outer loop.
Predict Output
intermediate
2:00remaining
Value of variable after for loop
What is the value of variable sum after running this code?
C
#include <stdio.h>

int main() {
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        sum += i;
    }
    printf("%d", sum);
    return 0;
}
A10
B15
C5
D0
Attempts:
2 left
💡 Hint
Add numbers from 0 to 4.
🔧 Debug
advanced
2:00remaining
Identify the error in the for loop
What error does this code produce when compiled or run?
C
#include <stdio.h>

int main() {
    int i;
    for (i = 0; i < 5; i++)
        printf("%d ", i);
    i = i + 1;
    return 0;
}
ANo error, prints: 0 1 2 3 4
BCompilation error: missing braces
CRuntime error: segmentation fault
DNo output
Attempts:
2 left
💡 Hint
Check if braces are required for single statement loops.
Predict Output
advanced
2:00remaining
Output of a for loop with continue
What is the output of this code?
C
#include <stdio.h>

int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 2) continue;
        printf("%d", i);
    }
    return 0;
}
A01234
B0134
C024
DError: continue outside loop
Attempts:
2 left
💡 Hint
The continue skips printing when i is 2.
🧠 Conceptual
expert
2:00remaining
Number of iterations in a complex for loop
How many times will the loop body execute in this code?
C
#include <stdio.h>

int main() {
    int count = 0;
    for (int i = 1; i < 10; i *= 3) {
        count++;
    }
    printf("%d", count);
    return 0;
}
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Look at how i changes each time: 1, 3, 9, then stops.