Articles

C++ program to find reverse of a number

C++ program to find reverse of a number


In this chapter of C++ program tutorial out task is: 

  • Write a C++ program to find the reverse of the number.

C++ program to find the reverse of the number

Below is the C++ program to find reverse of the number.

#include<iostream>
using namespace std;
int main()
{
    int number;
    cout<<"Enter the number:"<<endl;
    cin>>number;
    int reverseNumber=0,remainder;
    while(number!=0){
        remainder = number % 10;
        reverseNumber = reverseNumber * 10 + remainder;
        number = number / 10;
    }
    cout<<"The reverse of the given number is"<<" "<<reverseNumber<<endl;
    return 0;
}

OUTPUT :

Enter the number:

1678
The reverse of the given number is 8761
 
EXPLANATION :
In the above program:
  • The execution starts from the main function (here int main()).
  • A 4 bit memory space is created for each of the integers 'number','reverseNumber ','remainder'(assuming 64 bit system).
  • The 'reverseNumber' variable is assigned with value 0 initially or it will take garbage value and give undesired output.
  • In the while statement it is checking that it will run the loop untill the number becomes 0.
  • In the 'remainder' variable it is extracting the last digit and storing it in the 'reverseNumber' as the first digit.
  • Then that digit is removed from the 'number' variable and the loop continues.
  • Then the reverse of the number is given as the output.
  • The program than gives the desired output and terminates in the line "return 0" returning a 0 value.

WORKING :

number = 1678

while(1678!=0)

{

remainder = 1678 % 10       // remainder  = 8

reverseNumber = 0 * 10 + 8    //reverseNumber = 8

number = 1678 / 10    // number = 167

}

next loop :

number = 167

while(167!=0)

{

remainder = 167 % 10       // remainder  = 7

reverseNumber = 8 * 10 + 7    //reverseNumber = 87

number = 167 / 10    // number = 16

}

next loop :

number = 16

while(16!=0)

{

remainder = 16 % 10       // remainder  = 6

reverseNumber = 87 * 10 + 6    //reverseNumber = 876

number = 16 / 10    // number = 1

}

next loop :

number = 1

while(1!=0)

{

remainder = 1 % 10       // remainder  = 1

reverseNumber = 876 * 10 + 1    //reverseNumber = 8761

number = 1 / 10    // number = 0

}

The loop terminates as the number becomes 0 

 

You can see the above code execution and output in codeblocks IDE:

C++ program to find the reverse of the number

 


CPP Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sayantan Bose
- šŸ”­ I’m currently working on DS Algo skills - 🌱 I’m currently learning web develeopement - šŸ‘Æ I’m looking to collaborate with Oppia - šŸ“« Reach me at https://www.linkedin.com/in/sayantan-bose-14134a1a6/ - šŸ˜„ Pronouns: his/him
Page Views :    Published Date : Aug 23,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!