0
0
CProgramBeginner · 2 min read

C Program to Reverse a Number with Output and Explanation

To reverse a number in C, use a loop to extract the last digit with num % 10, build the reversed number by rev = rev * 10 + digit, and remove the last digit with num = num / 10 until num becomes zero.
📋

Examples

Input1234
Output4321
Input1000
Output1
Input7
Output7
🧠

How to Think About It

To reverse a number, think of peeling off the last digit repeatedly using the remainder operator %. Each digit you get, add it to a new number that you build by shifting its digits left (multiply by 10) and adding the new digit. Keep doing this until the original number is fully processed.
📐

Algorithm

1
Get input number from the user
2
Initialize reversed number as 0
3
While the input number is not zero:
4
Extract last digit using remainder operator
5
Add this digit to reversed number after shifting digits left
6
Remove last digit from input number by dividing by 10
7
Print the reversed number
💻

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

1

Initial values

num = 1234, rev = 0

2

First loop iteration

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

3

Second loop iteration

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

4

Third loop iteration

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

5

Fourth loop iteration

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

6

Loop ends

num = 0, reversed number = 4321

numdigitrev
123444
123343
122432
114321
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number, like peeling one digit off.

Step 2: Build reversed number

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

Step 3: Remove last digit

Divide the original number by 10 using integer division to drop the last digit and continue the process.

🔄

Alternative Approaches

Using recursion
c
#include <stdio.h>

int reverse(int num, int rev) {
    if (num == 0) return rev;
    return reverse(num / 10, rev * 10 + num % 10);
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Reversed number: %d\n", reverse(num, 0));
    return 0;
}
Recursion is elegant but uses more memory due to function calls.
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--) {
        putchar(str[i]);
    }
    printf("\n");
    return 0;
}
This method treats the number as text, 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 time depends on the number of digits d.

Space Complexity

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

Which Approach is Fastest?

The iterative method is fastest and uses least memory; recursion adds overhead, and string method is less numeric.

ApproachTimeSpaceBest For
Iterative (mod/div)O(d)O(1)Numeric reversal, efficient
RecursionO(d)O(d)Elegant code, but uses stack memory
String reversalO(d)O(d)Simple for text, not numeric operations
💡
Use integer division and modulus operators to peel digits one by one for reversal.
⚠️
Forgetting to update the original number inside the loop causes an infinite loop.