Today we will learn a C++ Program to Reverse a Number and Program to reverse a Number in C++ using different loops and methods. First of all,

What is Reverse of a Number?

Reversing a number means changing all the positions of the digit of a number.

For Example 12345 and it’s reverse is 54321.

There are many methods to reverse a number in C++ we will see it one by one.

1. C++ Program to Reverse a Number Using While Loop

In this program, the compiler will ask the user to enter the number which the user wants to reverse a number. After reversing the number the compiler will print output on the screen. We will use while loop for reversing a number.

//Learnprogramo - programming made simple
#include <iostream>  
using namespace std;  
int main()  
{  
int num, rev=0,rem;    
cout<<"Please Enter Your  number : ";    
cin>>num;    
  while(num!=0)    
  {    
     rem=num%10;      
     rev=rev*10+rem;    
     num/=10;    
  }    
 cout<<"Here is the Reversed Number: "<< rev <<endl;     
return 0;  
}  

Output:

c++ program to reverse a number

Explanation:

Let us consider user enter the number 1456. First the value of rev is 0.

1st Iteration: rem=n%10 i.e rem=1456%10=6 and rev=rev*10+rem i.e rev=0*10+6=0+6=6.So, n=n/10 i.e 1456/10=145.

2nd Iteration: from the first iteration the value of n=145 and rev=6. rem=n%10 i.e rem=145%10=5 and rev=rev*10+rem i.e rev=6*10+5=60+5=65. So, n=n/10 i.e 145/10=14.

3rd Iteration: from the second iteration the value of n=14 and reverse=65. rem=n%10 i.e rem=14%10=4 and the rev=rev*10+rem i.e rev=65*10+4=650+4=654. So, n=n/10=14/10=1.

4th Iteration: from the third iteration the value of n=1 and rev=654. rem=n%10 i.e rem=1%10=1 and rev=rev*10+rem i.e rev=654*10+1=6540+1=6541. So,n=n/10 i.e n=1/10=0

Now the value of n is 0 so the while loop will exit.

2. Logic to reverse a Number Without Using Loop

consider the number 123.
Now perform 123%10 = 3 = a
perform 123/10 =12
if not 0,then multiply a with 10, so a = 30
Now perform 12%10 = b
perform 12/10 = 1
if not 0, then multiply b with 10 & a with 10,so a = 300 & b = 20
perform 1%10 = c
perform 1/10 = 0
if not 0, continue….however, as this becomes 0, you can stop the process.
just add a+b+c. = 300+20+1 = 321

3. C++ Program to Reverse a Number Using For Loop

In this program, we will use for loop to reverse a number entered by the user. Only the loop has been changed but, the logic behind the program is the same.

//Learnprogramo - programming made simple
#include<iostream>
using namespace std;
  int main()
  {
   int no,rev=0,r,a;
   cout<<"Enter any numb: ";
   cin>>no;
   a=no;
   for(;no>0;)
   {
    r=no%10;
    rev=rev*10+r;
    no=no/10;
   }
  cout<<"\nReverse of "<<a<<" is: "<<rev;
 }

Output:

c++ program to reverse a number

3. Using Recursive Method

//Learnprogramo - programming made simple
#include <iostream> 
using namespace std; 
int reversDigits(int num) 
{ 
static int rev_num = 0; 
static int base_pos = 1; 
if(num > 0) 
{ 
	reversDigits(num/10); 
	rev_num += (num%10)*base_pos; 
	base_pos *= 10; 
} 
return rev_num; 
} 
int main() 
{ 
	int num = 12345; 
	cout << "Reverse of no. is "
		<< reversDigits(num); 
	return 0; 
}

Output:

program to reverse a number in c++

Time Complexity of the above program is O(log(n)).

The above program does not consider leading zeros after a number eg. 500 program will consider as 5.

Also Read: