How to Reverse A Number in C++
The program given below is made to reverse the number. For example if number entered by user is 567 then program will reverse it to 765.
using namespace std;
cout<<"Enter Number: ";
cout<<"\nReversed Number: "<<reverse;
- To reverse a number, it is necessary to have access to every digit individually of entered number.
- This program uses while loop to access every digit of number.
- We used Modulus Operator to extract digits one by one from number and added extracted digits to reverse variable.
- Number is divided by 10 so for next loop iteration it will point to next digit.
- Reverse number is multiplied by 10 so digits extracted will be placed one place further. If we don’t do this then digits will be added up to produce sum.
- After the breakage of loop, all the digits are accumulated in reverse order in reverse variable.
Thanks for reading this article. If you have any question then you can ask me in comments.
You can also Click Here To Download Exercise Files.