0
0
CProgramBeginner · 2 min read

C Program to Reverse Number Using While Loop

You can reverse a number in C using a while loop by extracting digits with num % 10, building the reversed number with rev = rev * 10 + digit, and reducing the original number with num = num / 10 until it becomes zero.
📋

Examples

Input1234
Output4321
Input1000
Output1
Input7
Output7
🧠

How to Think About It

To reverse a number, think of peeling off its last digit one by one using the remainder operator %. Then, add each digit to a new number by shifting the existing digits left (multiply by 10) and adding the new digit. Repeat this until the original number is fully processed (becomes zero).
📐

Algorithm

1
Get the input number.
2
Initialize reversed number as 0.
3
While the input number is not zero:
4
Extract the last digit using modulus 10.
5
Multiply reversed number by 10 and add the extracted digit.
6
Remove the last digit from the input number by dividing by 10.
7
After the loop ends, the reversed number is ready.
💻

Code

c
#include <stdio.h>

int main() {
    int num, rev = 0, digit;
    printf("Enter a number: ");
    scanf("%d", &num);

    while (num != 0) {
        digit = num % 10;
        rev = rev * 10 + digit;
        num = num / 10;
    }

    printf("Reversed number: %d\n", rev);
    return 0;
}
Output
Enter a number: 1234 Reversed number: 4321
🔍

Dry Run

Let's trace the number 1234 through the code to see how it reverses.

1

Initial values

num = 1234, rev = 0

2

First iteration

digit = 1234 % 10 = 4; rev = 0 * 10 + 4 = 4; num = 1234 / 10 = 123

3

Second iteration

digit = 123 % 10 = 3; rev = 4 * 10 + 3 = 43; num = 123 / 10 = 12

4

Third iteration

digit = 12 % 10 = 2; rev = 43 * 10 + 2 = 432; num = 12 / 10 = 1

5

Fourth iteration

digit = 1 % 10 = 1; rev = 432 * 10 + 1 = 4321; num = 1 / 10 = 0

6

Loop ends

num is 0, reversed number is 4321

numdigitrev
123444
123343
122432
114321
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number.

Step 2: Build reversed number

Multiply the current reversed number by 10 to shift digits left, then add the extracted digit.

Step 3: Remove last digit

Divide the original number by 10 to remove the last digit and prepare for the next iteration.

🔄

Alternative Approaches

Using recursion
c
#include <stdio.h>

void reverse(int num) {
    if (num == 0) return;
    printf("%d", num % 10);
    reverse(num / 10);
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Reversed number: ");
    reverse(num);
    printf("\n");
    return 0;
}
This prints the reversed number directly without storing it, but uses function calls which may be less efficient for very large numbers.
Using string conversion
c
#include <stdio.h>
#include <string.h>

int main() {
    char str[20];
    printf("Enter a number: ");
    scanf("%s", str);
    int len = strlen(str);
    printf("Reversed number: ");
    for (int i = len - 1; i >= 0; i--) {
        printf("%c", str[i]);
    }
    printf("\n");
    return 0;
}
This treats the number as text and reverses characters, which is simple but not numeric reversal.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so the time complexity is O(d), where d is the number of digits.

Space Complexity

Only a few integer variables are used, so the space complexity is O(1), constant space.

Which Approach is Fastest?

The while loop method is efficient and uses constant space, while recursion adds call stack overhead and string conversion uses extra memory.

ApproachTimeSpaceBest For
While loopO(d)O(1)Numeric reversal with minimal memory
RecursionO(d)O(d)Simple code but uses call stack
String reversalO(d)O(d)When input is treated as text
💡
Always initialize your reversed number variable to zero before the loop.
⚠️
Forgetting to update the original number inside the while loop causes an infinite loop.