Table of Contents

**Introduction**

Reversing a number implies that the value of the unit of a given number will become the nth digit value of the number if it is n digits long, and the tens digit number will become (n-1)^{th} place value number and so on. It implies printing the digits of a specified number from the back to the front. For example, if the given number is 987, the reverse of that number is 789.

Now, before moving on, let us discuss its relevance. Sometimes we are stuck upon a code where we have to find the palindrome of a number. If you are unfamiliar with the concept of palindrome, then please note that a palindrome is a number that is the same even after its reversal.

For example, number 4554 is a palindrome of 4554. So, in this case, it becomes mandatory for us to fetch all the possible values which contain palindrome numbers, and hence here comes the need to find the reversal of the number.

Let’s look at some methods to implement its logic in different programming languages:

- Reverse a number using loops
- Reverse a number using recursion
- Reserve a string in C and C++

**Flowchart**

**Reverse a number using loops **

We will be using while loops for this approach. The loop continues till n becomes 0. Each time the last digit of n is extracted and added to the front of the **rev**. This process continues till the condition evaluates to false.

**Algorithm: **

- Take the number N as input from the user.
- Initialize the reverse number to 0.
- Run a while loop until N becomes 0, and in each iteration, pick up the last digit of the number N by taking modulo 10.
- Now, make it the first digit of the reverse number by multiplying the already existing reverse number by ten and then adding the last digit obtained to it.
- Pass N as N/10 to the next iteration.
- Print the reverse number as output.

**Pseudo Code:**

```
Input=N
rev_num=0
While input is greater than 0:
Last_digit=N modulo 10
N=N/10
rev_num=rev_num*10+last_digit
print(rev_num)
```

In the programs given below for finding the reverse of a number, we will be using a while loop ( a preconditioned loop) inside functions. The condition here is checked using the greater than (>) operator with the given number if it has a greater value than 0.

1.**C++ **

```
#include <bits/stdc++.h>
using namespace std;
int reverse(int n)
{
int revNum=0;
while(n>0)
{ revNum= revNum*10+ n%10;
n=n/10;
}
return revNum;
}
int main() {
int n=3456;
cout<<"The Reverse number is "<<reverse(n);
return 0;
}
Output
The Reverse number is 6543
```

2. **C**

```
#include <stdio.h>
int reverse(int n)
{
int revNum = 0;
while (n > 0) {
revNum = revNum * 10 + n % 10;
n = n / 10;
}
return revNum;
}
int main()
{
int n= 3456;
printf("The reverse number is %d", reverse(n));
return 0;
}
Output
The reverse number is 6543
```

3. **Java**

```
class result {
static int reverse(int n)
{
int revNum = 0;
while (n > 0) {
revNum = revNum * 10 + n % 10;
n = n / 10;
}
return revNum;
}
public static void main(String[] args)
{
int n=3456;
System.out.println("The reverse number is "
+ reverse(n));
}
}
Output
The reverse number is 6543
```

4. **Python**

```
n = 3456
revnum = 0
while(n > 0):
temp = n % 10
revnum = revnum * 10 + temp
n = n // 10
print('The reverse number is', revnum)
Output
The reverse number is 6543
```

Time Complexity is O(log(n)), where n is the input number.

Space Complexity is O(1) as we used constant space.

**Reverse a number using recursion **

A recursive function solves a problem by calling a copy of itself to work on a smaller problem. Each time a function calls itself, a simpler version of the original problem is shown. All these smaller problems converge on a base case.

The idea is to use recursion to perform the task for a single digit of the number, and let recursion handle all the numbers on its own.

**Algorithm: **

- Take the number N as input from the user.
- Create a function for reversing a number that accepts the input N as its parameter.
- Initialise the reverse number to 0.
- Check if N is greater than 0, and update the already existing reverse number by multiplying it by 10.
- And then add the reverse number with the last digit of the number N by taking modulo 10.
- Keep on calling the recursive function until N becomes less than 0.
- Finally, return the reverse number and print it as output.

**Pseudo Code:**

```
Input=N
Reverse (int N) //Recursive function accepting input as its parameter
rev_num=0
While input N is greater than 0:
rev_num=rev_num*10
rev_num=rev_num + N modulo 10
Reverse (N/10)
print(rev_num)
```

In the programs given below, we used recursive functions to reverse a number by using different conditions through while loops.

1.**C++**

```
#include <bits/stdc++.h>
using namespace std;
int reverse(int num)
{ static int revNum = 0;
if (num>0)
{revNum = revNum*10;
revNum += (num % 10);
reverse(num / 10);
return revNum;
}
else
return 0;
}
int main()
{ int num= 1289;
cout << reverse(num);
return 0;
}
Output
9821
```

2. **C**

```
#include <stdio.h>;
int reverse(int num)
{
static int revNum = 0;
if (num>0)
{revNum = revNum*10;
revNum += (num % 10);
reverse(num / 10);
return revNum;
}
else
return 0;
}
int main()
{
int num=1289;
printf(reverse(num));
return 0;
}
Output
9821
```

3. **Java**

```
class result {
static int revNum = 0;
static int reverse(int num)
{ if (num>0)
{ revNum = revNum*10;
revNum += (num % 10);
reverse(num / 10);
return revNum;
}
else
return 0;
}
public static void main(String[] args)
{ int num = 1289;
System.out.println(reverse(num));
}
}
Output
9821
```

4. **Python3**

```
revnum = 0
base_pos = 1;
def reverse(num):
global revnum
global base_pos
if(num > 0):
reverse((int)(num / 10))
revnum += (num % 10) * base_pos
base_pos *= 10
return revnum
num = 1289
print(reverse(num))
Output
9821
```

Time Complexity for all the iterative solutions is O(log(n)), where n is the input number and Space Complexity is O(1) as we used constant space. Now that you have learnt the approaches, try to solve the reverse number problem yourself.

## Frequently Asked Questions

**What is the logic to reverse a number?**

The program takes integer input from the user. Then the while loop is used until number != 0 is false. In each iteration of the loop, the remainder when the number is divided by 10 is calculated, and the value of the number is reduced by 10 times.

**How do you reverse a number without a loop?**

First, initialize two integer variables like (number, temp). Now perform the reverse operation with both the variables. Finally, show the result after reversing as output.

**Why does the 1089 trick work?**

If we let a, b, c denote the original number’s three digits, and then the three-digit number is 100a+10b+c. The reverse is 100c+10b+a. Now, subtract: (original number)-(reverse of number) to get 99(a-c).

As the digits are decreasing, (a-c) will be at least 2 and cannot be greater than 9, so the result must be one of the following 198, 297, 396, 495, 594, 693, 792, or 891. If you add any one of these numbers to the reverse of itself, you get 1089.

For example, if you start with 654 (three digits, decreasing order), then the reverse is 456. Subtract 654-456 to get 198. Now add 198, and it’s reverse 891, and you will get 1089!

**Key Takeaways**

In this article, we studied the ways we can reverse the digits of a number. This article uses both functions and recursion in languages like C++, C, Java, and Python. This is the most optimal and widely used approach for reversing the digits of a number.

The process is almost the same in every programming language, and therefore, once you know the logic of the program, it will be straightforward for you to implement it.

Now that you know how to implement the theory of reversing a number, you can try practicing this problem on CodeStudio to evaluate yourself.

**By Mehak Goel**

## Leave a Reply