0
0
Cprogramming~20 mins

Nested loops in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Loops Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested loops with break

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 <= 3; j++) {
            if (j == 2) break;
            printf("%d%d ", i, j);
        }
    }
    return 0;
}
A11 12 13 21 22 23 31 32 33
B11 21 31
C11 12 21 22 31 32
D11 21 22 31 32
Attempts:
2 left
💡 Hint

Remember that break exits the inner loop immediately when j == 2.

Predict Output
intermediate
2:00remaining
Counting iterations in nested loops

What is the value of count after running this code?

C
#include <stdio.h>

int main() {
    int count = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = i; j < 4; j++) {
            count++;
        }
    }
    printf("%d", count);
    return 0;
}
A12
B16
C8
D10
Attempts:
2 left
💡 Hint

Count how many times the inner loop runs for each i.

🔧 Debug
advanced
2:00remaining
Identify the error in nested loops

What error does this code produce when compiled?

C
#include <stdio.h>

int main() {
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            printf("%d %d\n", i, j);
    printf("Done\n");
    return 0;
}
ACompilation error: expected ';' before 'printf'
BNo error, prints pairs and 'Done' three times
CNo error, prints pairs and 'Done' once
DRuntime error: segmentation fault
Attempts:
2 left
💡 Hint

Check how the indentation affects which statements belong to the loops.

🧠 Conceptual
advanced
2:00remaining
Number of iterations in nested loops with conditions

What value does this program print?

C
#include <stdio.h>

int main() {
    int count = 0;
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= 5; j++) {
            if (i * j % 2 == 0) {
                count++;
            }
        }
    }
    printf("%d", count);
    return 0;
}
A16
B10
C25
D15
Attempts:
2 left
💡 Hint

Count pairs (i, j) where product is even.

Predict Output
expert
2:00remaining
Output of nested loops with continue and multiple conditions

What is the output of this code?

C
#include <stdio.h>

int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (i == j) continue;
            if (i + j == 4) continue;
            printf("%d%d ", i, j);
        }
    }
    return 0;
}
A12 21 23 32
B12 13 21 31 32
C13 21 31 32
D12 13 21 23 31 32
Attempts:
2 left
💡 Hint

Skip pairs where i == j or i + j == 4.