Reverse The Number

Wednesday, September 11, 2013 6 Comments A+ a-

C++ program to reverse the number


Problem:
Write a C++ program which ask for a number and reverse its digits.
Ex. Number = 1234 , the answer will be 4321
In order to reverse the number, we have to get the every digit starting from the rightmost or the last digit. In the above example it is number 4, then next is 3, then 2 until 1.

Steps to extract each digit and construct the reversed value of the number:

1) Extract the last digit through the use of modulus operator (%) to get the remainder when the a number is divided by 10.
2) Add the extracted digit to a reversed number variable (ex. reversedNum) which will be constructed. The variable should be initialized to 0 and must be multiplied with 10 before the extracted digit is added.
3) After the last digit is extracted, divide the number with 10 to eliminate the extracted digit and start again to Step 1. The cycle continues until there is number is already 0.

The following program shows how to reverse the digits of a number.

/*
Reverse the number
*/
#include <iostream>

using namespace std;

int main(void)
{
 long number, remainder, reverseNumber = 0;
 char hang;

 cout<< "Enter the number: ";
 cin>>number;
 
 while(number)
 {
  //extract the last digit from the number
  remainder = number % 10;  

  //build the reverseNumber digit by digit
  reverseNumber = (reverseNumber * 10) + remainder;

  //exclude the last digit from the number
  //ready for the next evaluation
  number = number / 10;    
 }

 cout<< "Reversed number = " << reverseNumber <<endl;
 
 //to let console stay
 cin>>hang;
 
 return 0;
}
Output of the program:

//Sample 1

Enter the number: 1235
Reversed number = 5321

//Sample 2

Enter the number: 1023
Reversed number = 3201

//Sample 3

Enter the number: 5007
Reversed number = 7005

//*Sample 4

Enter the number: 012
Reversed number = 21

//*Sample 5

Enter the number: 210
Reversed number = 12

*Note that for inputs having 0 in the beginning and in the end, 0 will not be include in the output, See below:
Input = 012        Output = 21 ( 012 is considered as 12 because the 0 is not significant)
Input = 210        Output = 12 ( instead of 012, 0 is not significant in the answer)

6 comments

Write comments
Unknown
AUTHOR
December 15, 2013 at 4:00 AM delete This comment has been removed by the author.
avatar
Unknown
AUTHOR
December 15, 2013 at 4:11 AM delete

Please,could you explain why you used the " while(number) " - what is the function of it.
And one other question,how to write the code source for typing also zeros
for ex. when we write the number 012 to get the reserved number 210.

Reply
avatar
Vinster
AUTHOR
March 7, 2014 at 8:05 AM delete

Hi John, Sorry for the late reply. while(number) means that we'll gonna continue the iteration while the number is not yet equal to 0 or when number is still more than 0. If you want to get the 0, you might you want to consider processing it as a string. I'll create a program and post it asoon here.

Reply
avatar
hitesh kumar
AUTHOR
December 12, 2015 at 11:07 AM delete

C++ Program to Reverse a Number

Reverse of number means reverse the position of all digits of any number. For example reverse of 536 is 635

Reply
avatar
hitesh kumar
AUTHOR
December 14, 2015 at 8:41 AM delete

C++ Program to Reverse a Strings

Reverse a String means reverse the position of all character of String. You can use swapping concept to reverse any string in c++.

Reply
avatar
suman
AUTHOR
March 21, 2019 at 7:22 AM delete

nice article for beginners.thank you.
welookups C++
javacodegeeks

Reply
avatar