ComputerShikshak.com

C++ - Reverse the Digits of a Number

Problem Statement

Write a program in C++ to reverse the digits present in an integer number entered by the user.

Brief Description

According to the problem statement we have to write a program in C++ that will take an integer value as input from the user through the keyboard and will to reverse the digits present in that number.

For example, if the user enters 157 as input, the program will generate 751 as output.

Program

				
					#include<iostream>
using namespace std;
int main()
{
	int num,temp,rev=0,rem;
	cout<<"Enter a number: ";
	cin>>num;
	temp=num;
	while(temp!=0)
	{
		rem=temp%10;
		rev=rev*10+rem;
		temp=temp/10;
	}
	cout<<"Reverse of "<<num<<" is "<<rev;
	return 0;
}
				
			

Output

Enter a number: 157
Reverse of 157 is 751

Explanation

In this program:
  • 4 integer variable namely num, temp, rev and rem are declared.
  • Initially 0 is stored in rev.
  • The integer value entered by the user is stored in num.
  • A copy of the entered value is stored in temp.
The step-by-step working of the while loop (when num and temp both contains 157) is shown in the table given below:
numtemp!=0rem=temp%10;remrev=rev*10+rem;revtemp=temp/10;tempComment
157----0-157Initial Values
157157!=0
↓
1(True)
rem=157%10;7rev=0*10+7;7temp=157/10;15Iteration 1
15715!=0
↓
1(True)
rem=15%10;5rev=7*10+5;75temp=15/10;1Iteration 2
1571!=0
↓
1(True)
rem=1%10;1rev=75*10+1;751temp=1/10;0Iteration 3
1570!=0
↓
0(False)
------While
loop
stops
working
as
temp!=0
is
evaluated
to
0(False)
While executing the while loop:
  • At first value of temp is checked to see whether it is 0 or not.
  • The condition temp!=0 will produce either 1(True) or 0(False) after evaluation.
    • If it is 1(True), the statements written within while loop are executed and the loop continues to iterate till the condition is 1(True).
    • If it is 0(False) then the statements written within while loop are not executed and the control goes to the statement written immediately after the loop.
  • Within while loop, at first the last digit is extracted from the value stored in temp by performing modulo division operation using the expression temp%10. Thus the expression temp%10 will give the remainder after integer division which is the last digit of the value stored in temp and the result is stored in rem. 

  • After that the value stored in rev is multiplied with 10 and the value obtained in rem is added with it and the result is stored in the same variable i.e. rev. The expression rev=rev*10+rem; is used to construct a number from individual digits.

  • The value stored in temp is divided by 10 to remove the last digit of the value stored in it and the result is stored in the same variable i.e. temp.

Thus the above process is used to reverse the digits present in the given number.

The final result is stored in rev. 

Finally the result along with the value entered by the user is displayed on the output screen.

Note:-
If we do not store a copy of the value entered by the user in temp and perform the whole computation using the value stored in num then while displaying the final result we would not be able to display the entered value along with the result as value of num would become 0 after computation(exactly like the value of temp).
Share this page on
Scroll to Top