# How to Reverse a number in C++

In this tutorial, we will learn how to reverse a number in c++. The Reverse of a number means to reverse the position of all digits of any number and storing the digits in reverse order. For example, the reverse of 254 is 452.

We can reverse a number in c++ using a loop and arithmetic operators. In this program, we will get a number as input from the user and will reverse that number.
Let’s see the explanation of the program over here. These are some simple steps to code the program.

Program Explanation:

• Firstly, we will check the number if it is greater than zero or not.
• Then we will find the remainder of the given number and will get the last digits of the number.
• After that, we will place the last digit in the first position.
• Again we divide the number by 10 and store the value in a variable.
• Now we will get a new number which will have all digits except for the last digit.
• We will continue to check while loop again and again and find the remainder and get last digits of the number.
The same process is repeated again and again until the condition is true.
For any program, try to reduce time complexity and space complexity.

Time complexity –  is O(Log(n))  where n is the input number.

## Reverse a number in C++

```#include<iostream>
using namespace std;
//function to reverse digits of a number
int reversenumber(int num)
{
int rev_no=0;
while(num>0)
{
rev_no = rev_no*10 + num%10;
num=num/10;

}
return rev_no;
}
int main()
{
int n;
cin>>n;
// print number
cout<<"number"<<endl;
cout<<n<<endl;

//print reverse of number
cout<<reversenumber(n)<<endl;
return 0;
}```

OUTPUT EXAMPLE :

```INPUT:  12345
OUTPUT:  54321

INPUT:  762419
OUTPUT:  914267```