Bird
Raised Fist0
Cprogramming~20 mins

While loop in C - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does a while loop do in C programming?
easy
A. Repeats a block of code while a condition is true
B. Executes code only once regardless of condition
C. Runs code a fixed number of times without checking condition
D. Stops the program immediately

Solution

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

    A while loop runs repeatedly as long as its condition remains true.
  2. Step 2: Compare options with the definition

    Only Repeats a block of code while a condition is true correctly describes this behavior.
  3. Final Answer:

    Repeats a block of code while a condition is true -> Option A
  4. Quick Check:

    While loop = repeat while condition true [OK]
Hint: While loops run only if condition is true before each repeat [OK]
Common Mistakes:
  • Thinking while loop runs at least once even if condition is false
  • Confusing while loop with for loop fixed iterations
  • Believing while loop stops program immediately
2. Which of the following is the correct syntax for a while loop in C?
easy
A. while (condition) { /* code */ }
B. while condition { /* code */ }
C. while [condition] { /* code */ }
D. while: (condition) { /* code */ }

Solution

  1. Step 1: Recall C while loop syntax

    The condition must be inside parentheses and the code block inside braces.
  2. Step 2: Check each option

    Only while (condition) { /* code */ } uses correct parentheses and braces syntax.
  3. Final Answer:

    while (condition) { /* code */ } -> Option A
  4. Quick Check:

    Correct syntax uses parentheses and braces [OK]
Hint: Remember: while(condition) with parentheses and braces [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Using square brackets instead of parentheses
  • Adding colon after while keyword
3. What is the output of this C code?
int i = 1;
while (i <= 3) {
    printf("%d ", i);
    i++;
}
medium
A. 0 1 2 3
B. 1 2 3 4
C. 3 2 1
D. 1 2 3

Solution

  1. Step 1: Trace the loop variable values

    i starts at 1, prints 1, then increments to 2, prints 2, then 3, and stops after printing 3 because i becomes 4 and condition fails.
  2. Step 2: Confirm printed output

    The printed numbers are "1 2 3 " with spaces after each number.
  3. Final Answer:

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

    Prints 1 to 3 with spaces [OK]
Hint: Check loop start, end condition, and increments carefully [OK]
Common Mistakes:
  • Including 4 in output by mistake
  • Starting from 0 instead of 1
  • Printing numbers in reverse order
4. Identify the error in this code snippet:
int i = 0;
while (i < 5)
    printf("%d ", i);
medium
A. Syntax error due to missing braces
B. Missing semicolon after while condition
C. Infinite loop because i is never updated
D. Variable i is not declared

Solution

  1. Step 1: Analyze loop behavior

    Variable i starts at 0 and condition is i < 5, but i is never incremented inside the loop.
  2. Step 2: Understand consequence

    Since i never changes, the condition stays true forever, causing an infinite loop.
  3. Final Answer:

    Infinite loop because i is never updated -> Option C
  4. Quick Check:

    Loop variable must change to avoid infinite loop [OK]
Hint: Always update loop variable inside while loop [OK]
Common Mistakes:
  • Assuming loop ends without variable update
  • Thinking missing braces cause syntax error here
  • Ignoring infinite loop possibility
5. How can you modify this code to print all even numbers from 2 to 10 using a while loop?
int num = 1;
while (num <= 10) {
    printf("%d ", num);
    num++;
}
hard
A. Use a for loop instead of while loop
B. Initialize num to 2 and increment by 2 inside the loop
C. Initialize num to 0 and increment by 1 inside the loop
D. Keep num at 1 and print only if num % 2 == 0 without incrementing

Solution

  1. Step 1: Set starting point and increment for even numbers

    Start num at 2 (first even number) and increase by 2 each time to get only even numbers.
  2. Step 2: Modify loop accordingly

    Change initialization to num = 2 and inside loop do num += 2 instead of num++.
  3. Final Answer:

    Initialize num to 2 and increment by 2 inside the loop -> Option B
  4. Quick Check:

    Start at 2, add 2 each time for evens [OK]
Hint: Start at first even, add 2 each loop to get evens [OK]
Common Mistakes:
  • Not incrementing num causing infinite loop
  • Printing odd numbers by starting at 1
  • Using for loop when question asks for while loop
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