Reverse a Number in C

Table of Contents
 Reverse a Number in C
 Understanding the Problem
 Approach 1: Using Arithmetic Operations
 Approach 2: Using String Conversion
 Approach 3: Using Recursion
 Q&A
 Q1: Can this approach handle negative numbers?
 Q3: Can we reverse a floatingpoint number using these approaches?
 Q4: Are there any builtin functions in C to reverse a number?
When it comes to programming, reversing a number is a common task that often arises. Whether you are a beginner or an experienced programmer, understanding how to reverse a number in C can be a valuable skill to have. In this article, we will explore various methods and techniques to reverse a number in C, providing you with a comprehensive guide to tackle this problem efficiently.
Understanding the Problem
Before diving into the implementation details, let’s first understand what it means to reverse a number. Reversing a number simply involves changing the order of its digits. For example, if we reverse the number 123, we would get 321.
Approach 1: Using Arithmetic Operations
One of the simplest ways to reverse a number in C is by using arithmetic operations. Here’s a stepbystep approach:
 Initialize a variable to store the reversed number, let’s call it
reversedNum
, and set it to 0.  Extract the last digit of the given number using the modulo operator (
%
) and store it in a variable, let’s call itlastDigit
.  Multiply the
reversedNum
by 10 and add thelastDigit
to it.  Remove the last digit from the given number by dividing it by 10.
 Repeat steps 24 until the given number becomes 0.
 The final value of
reversedNum
will be the reversed number.
Let’s see this approach in action with an example:
#include <stdio.h>
int reverseNumber(int num) {
int reversedNum = 0;
while (num != 0) {
int lastDigit = num % 10;
reversedNum = reversedNum * 10 + lastDigit;
num /= 10;
}
return reversedNum;
}
int main() {
int num = 12345;
int reversedNum = reverseNumber(num);
printf("Reversed number: %dn", reversedNum);
return 0;
}
Output:
Reversed number: 54321
Approach 2: Using String Conversion
Another approach to reverse a number in C is by converting it to a string and then reversing the string. Here’s how you can do it:
 Convert the number to a string using the
sprintf()
function.  Reverse the string using a loop.
 Convert the reversed string back to an integer using the
atoi()
function.
Let’s see this approach in action with an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int reverseNumber(int num) {
char str[20];
sprintf(str, "%d", num);
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length  i  1];
str[length  i  1] = temp;
}
return atoi(str);
}
int main() {
int num = 12345;
int reversedNum = reverseNumber(num);
printf("Reversed number: %dn", reversedNum);
return 0;
}
Output:
Reversed number: 54321
Approach 3: Using Recursion
Recursion is another powerful technique that can be used to reverse a number in C. Here’s how you can implement it:
 Define a recursive function, let’s call it
reverseNumber()
, that takes the number as an argument.  If the number is less than 10, return the number itself.
 Otherwise, calculate the last digit of the number using the modulo operator (
%
).  Divide the number by 10 to remove the last digit.
 Call the
reverseNumber()
function recursively with the updated number.  Multiply the result of the recursive call by 10 and add the last digit to it.
 Return the final result.
Let’s see this approach in action with an example:
#include <stdio.h>
int reverseNumber(int num) {
if (num < 10) {
return num;
}
int lastDigit = num % 10;
int remainingNum = num / 10;
int reversedNum = reverseNumber(remainingNum);
return reversedNum * 10 + lastDigit;
}
int main() {
int num = 12345;
int reversedNum = reverseNumber(num);
printf("Reversed number: %dn", reversedNum);
return 0;
}
Output:
Reversed number: 54321
Q&A
Q1: Can this approach handle negative numbers?
A1: Yes, all the approaches mentioned above can handle negative numbers as well. The sign of the number will remain the same, and only the digits will be reversed.
Q2: What happens if the reversed number exceeds the range of int
data type?
A2: If the reversed number exceeds the range of the int
data type, it will result in an overflow. To handle larger numbers, you can use a larger data type, such as long long
or unsigned long long
.
Q3: Can we reverse a floatingpoint number using these approaches?
A3: No, these approaches are specifically designed for reversing integers. Reversing a floatingpoint number involves a different set of operations.
Q4: Are there any builtin functions in C to reverse a number?
A4: No, C does not provide any builtin functions to reverse a number. However, you can create your own functions or use the approaches mentioned above to achieve the desired result.