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
whileloop 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.
#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.
Output of the Reverse a Number Program
When you run this code and enter '1234', you'll see:
Enter a number: 1234
Reversed Number: 4321
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.
- Read the integer
nfrom the user. - Initialize
reverseto0. - Repeat while
nis not zero: - Extract the last digit of
nbyn % 10. - Multiply
reverseby 10 and add the digit. - Remove the last digit from
nby dividing by 10. - Continue until the number
nbecomes zero. - 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])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 theprintfandscanffunctions. - The
main()function begins execution. It declares integer variablesn,reverse, andrem. - The program prompts you to enter a number and reads it using
scanf. - The program enters a
whileloop that continues untilnbecomes zero. - Inside the loop, the remainder
remis calculated usingn % 10, which gets the last digit ofn. - The variable
reverseis updated to store the digits in reversed order. - The variable
nis divided by 10 to drop the last digit. - The loop repeats this process until
nis 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 answerWhat operation extracts the last digit of the number in this program?
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.
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Count Number of Digits in an Integer
- C Program to Check Whether a Number is Palindrome or Not
- C Program to Find Factorial of a Number
- C Program to Display Fibonacci Sequence with Example
- C Program to Add Two Integers with Explanation and Output
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.