Skip to content
StudyFame

while Loop in C

A for loop is great when you know how many times to repeat. But what about "keep going until the number becomes 0" or "keep asking until the user types the right PIN"? Here you know the stopping condition, not the count.

That's the job of the while loop. In this lesson you will learn it and use it to reverse a number, one of the most asked C programs, with a full dry run.

What You Will Learn

  • The syntax and flowchart of the while loop.
  • When to use while instead of for.
  • The digit trick: % 10 and / 10 in a loop.
  • Reverse a number program with a dry run.
  • Count digits and check a palindrome number.

while Loop in C Explained

Exam definition: The while loop is an entry-controlled loop that repeats its body as long as the condition is true. It is used when the number of iterations is not known in advance.

Memory trick: Read while (n != 0) as "as long as n is not 0, keep going". Like eating chips: while the packet is not empty, take one more.

Syntax of while Loop in C

initialization;
while (condition)
{
    /* body */
    update;
}

Flowchart of the while loop

  +--> < condition? > --False--> [ exit loop ]
  |          | True
  |          v
  |   [ body + update ]
  |          |
  +----------+

The digit trick

  • n % 10 gives the last digit (1234 % 10 = 4).
  • n / 10 removes the last digit (1234 / 10 = 123).
  • Repeat both while n != 0, and you visit every digit from right to left.

Example 1: Reverse a Number in C Using while Loop

#include <stdio.h>

int main(void)
{
    int n, reversed = 0, digit;

    printf("Enter a number: ");
    scanf("%d", &n);

    while (n != 0)
    {
        digit = n % 10;                    /* take the last digit   */
        reversed = reversed * 10 + digit;  /* add it to the reverse */
        n = n / 10;                        /* remove the last digit */
    }

    printf("Reversed number = %d\n", reversed);
    return 0;
}

Output

Enter a number: 1234
Reversed number = 4321

Dry Run: Reversing 1234

Iterationn (start)digit = n % 10reversed = reversed × 10 + digitn = n / 10
1123440 × 10 + 4 = 4123
212334 × 10 + 3 = 4312
312243 × 10 + 2 = 4321
411432 × 10 + 1 = 43210
–0Condition n != 0 is false → exit

The loop ran 4 times because 1234 has 4 digits. We never had to tell it "4"; the condition decided.

How the while Loop Works

  1. The condition is checked before every iteration.
  2. If true, the body runs; if false, the loop ends and the program continues after it.
  3. Something inside the body must change the condition (here, n = n / 10), or the loop never stops.
  4. If the condition is false at the start (n = 0), the body runs zero times. That's why for input 0 the reverse stays 0.

Example 2: Count Digits and Check a Palindrome Number

A palindrome number reads the same forwards and backwards, like 121 or 4554.

#include <stdio.h>

int main(void)
{
    int n, original, reversed = 0, count = 0;

    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;

    while (n != 0)
    {
        reversed = reversed * 10 + n % 10;
        n /= 10;
        count++;
    }

    printf("Digits: %d\n", count);
    if (original == reversed)
        printf("%d is a palindrome\n", original);
    else
        printf("%d is not a palindrome\n", original);

    return 0;
}

Output

Enter a number: 121
Digits: 3
121 is a palindrome

We save the number in original because the loop destroys n (it becomes 0). The same digit loop also gives the sum of digits in C.

Important Points to Remember

  • while is entry-controlled: it may run 0 times.
  • Use it when the stopping condition is known, not the count.
  • The body must change the condition, or it loops forever.
  • Digit loop: n % 10 (last digit), n / 10 (remove it).
  • Save the original value if you need it after the loop.

Common Mistakes Beginners Make

MistakeWhat happensFix
while (n != 0);Infinite loop (empty body; n never changes)Remove the semicolon
Forgetting n = n / 10Infinite loopUpdate inside the body
Comparing with n after the loopn is 0 by thenKeep a copy: original = n
reversed not set to 0Garbage resultint reversed = 0;

while Loop Practice Questions

  1. Write the syntax of the while loop.
  2. When should you use a while loop instead of a for loop?
  3. What do n % 10 and n / 10 give for n = 5872?
  4. Do a dry run of reversing 507.
  5. Why do we store the original number in the palindrome program?
Show answers
  1. init; while (condition) { body; update; }
  2. When the number of iterations is not known and the loop must run until a condition changes.
  3. 2 and 587.
  4. 507: digit 7 → rev 7, n 50; digit 0 → rev 70, n 5; digit 5 → rev 705, n 0; stop. Answer 705.
  5. Because the loop changes n to 0, so we need the original value to compare with the reverse.

Coding Exercise

Read a number and print the product of its digits using a while loop. Example: 234 → 2 × 3 × 4 = 24.

Show solution
#include <stdio.h>

int main(void)
{
    int n, product = 1;

    printf("Enter a number: ");
    scanf("%d", &n);

    while (n != 0)
    {
        product *= n % 10;
        n /= 10;
    }

    printf("Product of digits = %d\n", product);
    return 0;
}
Enter a number: 234
Product of digits = 24

Quick Quiz: while Loop in C (MCQ)

  1. A while loop checks its condition:

    A) After the body   B) Before the body   C) Only once   D) Never

  2. How many times does this run? int n = 0; while (n > 5) n++;

    A) 0   B) 5   C) 6   D) Infinite

  3. The reverse of 950 using the program above is:

    A) 059   B) 59   C) 950   D) 9

  4. Which of these is a palindrome number?

    A) 123   B) 1221   C) 1210   D) 100

  5. while (1) is:

    A) An error   B) Runs once   C) An infinite loop   D) Never runs

Show answers
  1. B: before the body.
  2. A: 0 (0 > 5 is false from the start).
  3. B: 59 (leading zeros are not printed for an int).
  4. B: 1221.
  5. C: an infinite loop (1 is always true).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Syntaxwhile (condition) { body; update; }
TypeEntry-controlled (may run 0 times)
Best forUnknown number of iterations
Last digitn % 10
Remove last digitn / 10
Reverse formularev = rev * 10 + n % 10
Infinite whilewhile (1)

Frequently asked questions

What is the difference between for and while?
for is best when the count is known; while when only the stopping condition is known. Both are entry-controlled.
How do you reverse a number in C?
while (n != 0): take n % 10, do rev = rev * 10 + digit, then n /= 10.
What is a palindrome number?
A number that is equal to its reverse, like 121 or 4554.
What causes an infinite while loop?
The condition never becomes false, usually a missing update or a semicolon after while().

Remember

  • while = "as long as" (eating chips until the packet is empty).
  • Digit loop: % 10 take, / 10 remove.

Next Topic

Next lesson: do-while Loop in C. You will learn the loop that always runs at least once, perfect for menus.