✓ Link copied to clipboard
C 📅 July 5, 2026 ⏱ 5 min read

C Program to Reverse a Number with Example

C Program to Reverse a Number with Example

Reversing a number might seem easy, but it’s pretty handy in programming. Think about needing to display a phone number backward for a fun app feature. You can totally do this once you know how to reverse digits. Here's how to reverse a number using C.

Prerequisites for the Reverse a Number Program in C

Make sure you know these basics before diving in:

  • How to use a while loop in C
  • Understanding of the % and / operators
  • Basic integer operations
  • Include the <stdio.h> library
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Reverse a Number

Below is the complete source code to reverse a number in C.

reverse_number.cC▶ Run
#include <stdio.h>

int main() {
    int n, reverse = 0, rem;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (n != 0) {
        rem = n % 10;
        reverse = reverse * 10 + rem;
        n /= 10;
    }
    printf("Reversed Number: %d\n", reverse);
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Reverse a Number Program

When you run this code and enter '1234', you'll see:

Output
Enter a number: 1234
Reversed Number: 4321
Output of C Program to Reverse a Number
Output of C Program to Reverse a Number

Algorithm to Reverse a Number in C

Reversing a number means processing its digits from last to first. Here is the exact logic the program uses, step by step.

  1. Read the integer n from the user.
  2. Initialize reverse to 0.
  3. Repeat while n is not zero:
  4. Extract the last digit of n by n % 10.
  5. Multiply reverse by 10 and add the digit.
  6. Remove the last digit from n by dividing by 10.
  7. Continue until the number n becomes zero.
  8. Print the reversed number.
flowchart TD
  A([Start]) --> B[/Read n/]
  B --> C[reverse = 0]
  C --> D{"n != 0?"}
  D -->|Yes| E["rem = n %% 10"]
  E --> F["reverse = reverse * 10 + rem"]
  F --> G[n = n / 10]
  G --> D
  D -->|No| H([End])
Flowchart: the algorithm step by step

How the C Program to Reverse a Number Works

Let's break down the code to see how it reverses a number.

  • The program starts by including the <stdio.h> library to use the printf and scanf functions.
  • The main() function begins execution. It declares integer variables n, reverse, and rem.
  • The program prompts you to enter a number and reads it using scanf.
  • The program enters a while loop that continues until n becomes zero.
  • Inside the loop, the remainder rem is calculated using n % 10, which gets the last digit of n.
  • The variable reverse is updated to store the digits in reversed order.
  • The variable n is divided by 10 to drop the last digit.
  • The loop repeats this process until n is zero, reversing the number.
  • Finally, the reversed number is printed using printf.
  • The function returns 0, indicating successful execution.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What operation extracts the last digit of the number in this program?

🎉Correct! Using 'n % 10' gives the last digit of the number.
🤔Not quite. Think about how you usually extract the last digit of a number.

Time and Space Complexity of C Program to Reverse a Number

Let's analyze how the program performs in terms of time and space.

Time Complexity: O(d). Here, d is the number of digits in the input number n — like 4 for 1234. This is because the loop iterates once per digit, seen in n /= 10. So, 10 digits mean 10 iterations.

Space Complexity: O(1). Only three integer variables n, reverse, and rem are used, and they occupy the same space, regardless of number size.

⚠️

Common mistake:Not initializing the reverse variable to zero can mess up your results.

Key takeaway:Reversing a number is just a simple loop that peels off digits from the end and puts them back in reverse order.

FAQs About Reversing a Number in C

Here are some common questions about reversing numbers in C.

No, this program only works with positive integers. You’ll need to tweak it for negative numbers.
The program will just output 0, since reversing 0 still gives you 0.
The % operator grabs the last digit, while / takes it out of the number.
You can switch the data type from int to long long to handle bigger numbers.
No, reversing floats or doubles isn’t that simple and needs a different method.
Sure, you might convert the number to a string and reverse that, but that adds more overhead.

If you found this program helpful, check out these related programs:

Conclusion

Reversing a number is a useful trick you'll often need in programming. Once you get the basic logic with loops, you’ll see it pop up in coding challenges and real-world scenarios. Dive deeper into more complex number manipulations to enhance your skills.

Check out more number-based coding challenges in C on our platform. Test yourself with our C language quiz and use the C online compiler to play around with your code.