How to Reverse a Number in C

How to Reverse a Number in C

3 mins read420 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Jun 10, 2024 11:51 IST

This article will discuss how to reverse a number in C programming using while loop, for loop, and recursion method.

2023_08_Feature-Image-Templates-90.jpg

In the programming world, we learn different concepts and methods to master it. One such concept is reversing a number. At first, it looks simple, but it can be quite tricky. So, in this article, we will learn how to reverse a number in C programming with the help of different examples.

When we talk about reversing a number, it means changing its order, i.e., If you have a number 123, then it reverses would be 321.

Now, let’s see how to do this.

Algorithm to Reverse a Number in C

  • Initialize a variable, say ‘reversedNumber’ = 0.
  • Multiply the reversed number by 10, add the remainder of the original number divided by 10 to ‘reversedNumber’.
    • i.e., reversedNumber = (reversedNumber) * 10 + (OriginalNumber) % 10
  • Divide the original number by 10.
    • Here, in this step the value of the original number gets updated by dividing it by 10. This will remove the last digit of the original number at each iteration and will terminate the loop once the value of the original number equals 0.

Now, let’s reverse the number 123 by using the above algorithm.

Number = 123
reversedNumber = 0
reversedNumber = reversedNumber * 10 + Number % 10 = 0 * 10 + 123 % 10 = 3
Number = Number/ 10 = 12
reversedNumber = reversedNumber * 10 + Number % 10 = 3 * 10 + 12 % 10 = 32
Number = Number/ 10 = 1
reversedNumber = reversedNumber * 10 + Number % 10 = 32 * 10 + 1 % 10 = 321
Number = Number/10 = 0.

Hence, the output will be 321.

Crack the code of C/C++ with our comprehensive guide; find details on top colleges, programmes and online courses.

Until now, you have an idea of how to reverse a number.
So, let’s write a program in C programming following the above algorithm.


 
#include <stdio.h>
int main() {
int num, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while(num != 0) {
remainder = num % 10;
reversedNumber = reversedNumber * 10 + remainder;
num /= 10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
Copy code

Output

Enter an integer: 123
Reversed Number = 321

Yes, we are getting the same result.

In the above example, we have used While loop to perform our task. Now, let’s see how to reverse a number in C using for loop and recursion method.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
40 hours
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
1 month
– / –
2 months
– / –
50 hours
5.5 K
140 hours

Reverse a Number in C using For Loop


 
#include <stdio.h>
int main() {
int num, reversedNumber = 0, remainder, temp;
// Prompt the user to enter an integer
printf("Enter an integer: ");
scanf("%d", &num);
// Store the original number in a temporary variable for the loop condition
temp = num;
// Explanation: The idea behind reversing a number is similar to the while loop method.
// We'll repeatedly extract the last digit of the number and append it to the reversedNumber.
// The difference here is that we'll use a for loop, which will run as long as the temporary variable is not zero.
// The for loop will run as long as temp is not zero
for(; temp != 0; temp /= 10) {
// Extract the last digit of the number
remainder = temp % 10;
// Append the extracted digit to the reversedNumber
reversedNumber = reversedNumber * 10 + remainder;
}
// Display the reversed number
printf("Reversed Number = %d", reversedNumber);
return 0;
}
Copy code

Reverse a Number in C using Recursion Method


 
#include <stdio.h>
#include <math.h>
// Function to count the number of digits in a number
int countDigits(int num) {
if (num == 0) {
return 0;
}
return 1 + countDigits(num / 10);
}
// Recursive function to reverse a number
int reverseNumber(int num, int len) {
// Base case: If the number is 0, return 0
if (num == 0) {
return 0;
}
// Extract the last digit
int lastDigit = num % 10;
// Add the last digit to the reversed number in its new position
// and continue the recursion with the rest of the number
return lastDigit * pow(10, len - 1) + reverseNumber(num / 10, len - 1);
}
int main() {
int num;
// Prompt the user to enter an integer
printf("Enter an integer: ");
scanf("%d", &num);
// Calculate the number of digits in the number
int len = countDigits(num);
// Reverse the number using recursion
int reversedNum = reverseNumber(num, len);
// Display the reversed number
printf("Reversed Number = %d", reversedNum);
return 0;
}
Copy code

Conclusion

Reversing a number, it means changing its order, i.e., If you have a number 123, then it reverses would be 321. In this article, we have briefly discussed how to reverse a number in C programming using while loop, for loop and recursion method.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

FAQs

What does it mean by reversing a number in C?

Reversing a number, it means changing its order, i.e., If you have a number 123, then it reverses would be 321.

Why is the modulus operator used in the reversal process?

The modulus operator helps extract the last digit of a number, i.e., when modulus operator is applied it gives the remainder of the number.

How does integer division play a role in the reversal algorithm?

Integer division by 10 is used to remove the last digit from the number. For instance, 12345 / 10 would yield 1234.

Can the reversal process handle negative numbers?

Yes, but with some modifications. One common approach is to handle the negative sign separately, reverse the positive part of the number, and then reattach the negative sign if needed.

What happens if the number ends with a zero, like 1200?

When reversed, the leading zeros are typically dropped, so 1200 would become 21.

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio