Bird
Raised Fist0
Cprogramming~20 mins

For loop in C - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does a for loop in C primarily do?
easy
A. Repeats a block of code a fixed number of times
B. Defines a new variable
C. Stops the program execution
D. Declares a function

Solution

  1. Step 1: Understand the purpose of a for loop

    A for loop is designed to repeat a block of code multiple times based on a condition.
  2. Step 2: Identify the correct description

    Among the options, only repeating code a fixed number of times matches the for loop's purpose.
  3. Final Answer:

    Repeats a block of code a fixed number of times -> Option A
  4. Quick Check:

    For loop = repeat code [OK]
Hint: For loops repeat code a set number of times [OK]
Common Mistakes:
  • Confusing for loop with variable declaration
  • Thinking for loop stops program
  • Mixing for loop with function declaration
2. Which of the following is the correct syntax for a for loop in C?
easy
A. for i = 0 to 5 { }
B. for (int i = 0; i++ < 5;) { }
C. for (i < 5; i = 0; i++) { }
D. for (int i = 0; i < 5; i++) { }

Solution

  1. Step 1: Review standard for loop syntax in C

    The correct syntax is: for (initialization; condition; update) { body }
  2. Step 2: Check each option

    for (int i = 0; i < 5; i++) { } matches the correct syntax with initialization, condition, and update in proper order.
  3. Final Answer:

    for (int i = 0; i < 5; i++) { } -> Option D
  4. Quick Check:

    Syntax = for(init; cond; update) [OK]
Hint: For loop syntax: for(init; condition; update) [OK]
Common Mistakes:
  • Using 'for i = 0 to 5' which is not C syntax
  • Swapping condition and initialization
  • Missing semicolons inside for parentheses
3. What is the output of this code?
for(int i = 1; i <= 3; i++) {
    printf("%d ", i);
}
medium
A. 3 2 1
B. 0 1 2 3
C. 1 2 3
D. 1 2 3 4

Solution

  1. Step 1: Analyze the loop range

    The loop starts at i=1 and runs while i is less than or equal to 3, incrementing i by 1 each time.
  2. Step 2: Determine printed values

    It prints i each time: 1, then 2, then 3, then stops.
  3. Final Answer:

    1 2 3 -> Option C
  4. Quick Check:

    Loop prints 1 to 3 [OK]
Hint: Check loop start, end, and increments carefully [OK]
Common Mistakes:
  • Starting loop at 0 instead of 1
  • Including an extra number beyond 3
  • Printing in reverse order
4. Identify the error in this for loop:
for(int i = 0; i < 5; i)
    printf("%d ", i);
medium
A. Missing semicolon after printf statement
B. Missing increment operator in for loop update
C. Incorrect loop condition syntax
D. Variable i not declared

Solution

  1. Step 1: Examine the for loop update part

    The update section is 'i' without increment operator, so i never changes.
  2. Step 2: Understand the effect of missing increment

    Without 'i++' or similar, the loop runs infinitely or behaves incorrectly.
  3. Final Answer:

    Missing increment operator in for loop update -> Option B
  4. Quick Check:

    Update must change loop variable [OK]
Hint: For loop update must change variable (e.g., i++) [OK]
Common Mistakes:
  • Forgetting to increment loop variable
  • Confusing semicolon placement
  • Assuming variable auto-increments
5. How can you use a for loop to print only even numbers from 2 to 10 inclusive in C?
hard
A. for(int i = 2; i <= 10; i += 2) { printf("%d ", i); }
B. for(int i = 0; i <= 10; i++) { if(i % 2 == 0) printf("%d ", i); }
C. for(int i = 2; i < 10; i++) { if(i % 2 == 0) printf("%d ", i); }
D. for(int i = 0; i <= 10; i += 3) { printf("%d ", i); }

Solution

  1. Step 1: Understand the goal

    We want to print even numbers from 2 to 10 inclusive.
  2. Step 2: Check each option

    for(int i = 2; i <= 10; i += 2) { printf("%d ", i); } starts at 2 and increments by 2, printing 2,4,6,8,10 exactly.
  3. Final Answer:

    for(int i = 2; i <= 10; i += 2) { printf("%d ", i); } -> Option A
  4. Quick Check:

    Increment by 2 prints evens directly [OK]
Hint: Increment by 2 to print evens directly [OK]
Common Mistakes:
  • Using wrong increment step
  • Off-by-one errors in loop condition
  • Printing odd numbers by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard