# C++ Program to Reverse a Number

In this post, we will develop a program to reverse a number in C++. If the number is 12345 then the reverse number is 54321.

To reverse a number we need to extract the last digit of the number and add that number into a temporary variable with some calculation, then remove the last digit of the number. Do these processes until the number becomes zero. To remove the last digit the modulus operator (%) will be helpful for us, and to remove the last digit division operator (/) can be used. To add the last digit first we need to multiply the previous value by 10.

Procedure to find reverse of a number,

1) Take a number
2) Declare a temporary variable and initialize it with 0
3) find the last digit of the number
4) multiply the temporary variable by 10
5) add that last digit to the temporary variable
6) remove the last digit of the number.
7) Repeat this process from 3 to 6 until the number becomes 0.

``````#include<iostream>
using namespace std;
int main()
{
// declare variables
int number, remainder, sum=0;

// take input
cout << "Enter a Positive Integer : ";
cin >> number;

// loop to repeat the process
while( number != 0 )
{
// find last digit of the number
remainder = number % 10;

// multiply temporary variable by 10
// add last digit to reverse variable
sum = (sum*10) + remainder;

// remove last digit
number /= 10;
}

// display result
cout << "Reverse = " << sum << endl;

return 0;
}``````

Output:-

Enter a Positive Integer: 123456
Reverse = 654321

## Program to Reverse a Number in C++ Using Recursion

A technique of defining the method/function that contains a call to itself is called the recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.

The base case for finding the reverse of the number,

`number == 0 then return 0;`

The general case for finding the reverse of the number,

`reverse = (reverse*10) + (num%10);return findReverse(num/10);`

``````#include<iostream>
using namespace std;

// function declaration
int findReverse(int);

// global variable
static int reverse;

// main function
int main()
{
// declare variables
int number, remainder, result;

// take input
cout << "Enter a Positive Integer : ";
cin >> number;

// find Reverse
result = findReverse(number);

// display result
cout << "Reverse = " << result << endl;

return 0;
}

// function to find reverse of number
// using recursion technique
int findReverse(int num)
{
if(num==0) return 0; //base case

else { // general case
reverse = (reverse*10) + (num%10);
return findReverse(num/10);
}
}``````

Based on this program you can also write a C++ program to check number is Palindrome number or not. If the reverse of a given number is equal to the same number then the number is called Palindrome number. Example:- 5225 is a palindrome number, the reverse of 5225 is the same number 5225.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!