0
0
Cprogramming~20 mins

While loop in C - Practice Problems & Coding Challenges

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

int main() {
    int i = 1;
    while (i <= 3) {
        printf("%d ", i);
        i++;
    }
    return 0;
}
A0 1 2 3
B1 2 3 4
C1 2 3
D1 2
Attempts:
2 left
💡 Hint
Look at the condition and how the variable i changes inside the loop.
Predict Output
intermediate
2:00remaining
While loop with break statement
What will this program print?
C
#include <stdio.h>

int main() {
    int i = 0;
    while (1) {
        if (i == 3) {
            break;
        }
        printf("%d ", i);
        i++;
    }
    return 0;
}
A0 1 2
B1 2 3
C0 1 2 3
D0 1 2 3 4
Attempts:
2 left
💡 Hint
The loop runs forever until the break condition is met.
🔧 Debug
advanced
2:00remaining
Identify the error in this while loop
What error does this code produce when compiled or run?
C
#include <stdio.h>

int main() {
    int i = 0;
    while (i < 5)
        printf("%d ", i);
        i++;
    return 0;
}
AInfinite loop printing 0 forever
BRuntime error: segmentation fault
CCompilation error: missing semicolon
DPrints 0 1 2 3 4 and exits normally
Attempts:
2 left
💡 Hint
Check which statements are inside the loop and which are not.
🧠 Conceptual
advanced
2:00remaining
While loop behavior with post-increment
What is the value of variable i after this loop finishes?
C
#include <stdio.h>

int main() {
    int i = 0;
    while (i++ < 5) {
        // empty body
    }
    printf("%d", i);
    return 0;
}
A0
B5
C4
D6
Attempts:
2 left
💡 Hint
Remember that i++ returns the value before incrementing.
🚀 Application
expert
2:00remaining
Count digits in an integer using while loop
What is the output of this program?
C
#include <stdio.h>

int main() {
    int n = 12345, count = 0;
    while (n > 0) {
        n = n / 10;
        count++;
    }
    printf("%d", count);
    return 0;
}
A0
B5
C6
D4
Attempts:
2 left
💡 Hint
Each division by 10 removes one digit from the number.