# C++ program to Reverse a Number

Write a C++ program to Reverse a Number with an example. In this C++ reverse number example, while loop checks whether the number is greater than 0.

• reminder = number % 10 – It gives the last digit of a number.
• reverse = reverse * 10 + reminder – It adds that last digit at first position. Because of reverse * 10, the value will add at 2nd, 3rd,…nth positions. Otherwise, it replaces the existing ones.
• number = number / 10 – it removes the last digit from the number.
```#include<iostream>

using namespace std;

int main()
{
int number, reminder, reverse = 0;

cout << "Please Enter the Number that you want to Reverse  =  ";
cin >> number;

while (number > 0)
{
reminder = number % 10;
reverse = reverse * 10 + reminder;
number = number / 10;
}
cout << "\nReverse of given Number = " << reverse;

return 0;
}```

## C++ program to Reverse a Number using For Loop

```#include<iostream>

using namespace std;

int main()
{
int number, reminder, reverse = 0;

cout << "Please Enter the Number that you want to Reverse  =  ";
cin >> number;

for (;number > 0; number = number / 10)
{
reminder = number % 10;
reverse = reverse * 10 + reminder;
}
cout << "\nReverse of given Number = " << reverse;

return 0;
}```
``````Please Enter the Number that you want to Reverse  =  67859

Reverse of given Number = 95876``````

C++ code to Reverse a Number using Recursive Function.

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

int reverseNumber(int number)
{
static int reminder, reverse = 0;

if (number > 0)
{
reminder = number % 10;
reverse = reverse * 10 + reminder;
reverseNumber(number / 10);
}

return reverse;

}
int main()
{
int num, rev = 0;

cout << "Please Enter the Number that you want to Reverse  =  ";
cin >> num;

rev = reverseNumber(num);
cout << "\nReverse of given Number = " << rev;

return 0;
}```
``````Please Enter the Number that you want to Reverse  =  478956

Reverse of given Number = 659874``````