Reverse Number in C++
Q. Write a C++ program for Reverse Number.
#include
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter the number: ";
cin >> n;
while (n > 0)
{
sum = sum * 10 + n%10;
n = n/10;
}
cout << "Reverse of number is : " << sum ;
return 0;
}
Q. Write an algorithm to Reverse Number in C++.
1. Read the input number. 2. Extract the last digit by `num % 10`. 3. Append that digit to the reversed number by `reversed = reversed * 10 + digit`. 4. Remove the last digit from `num` by `num /= 10`. 5. Repeat until `num` becomes zero. 6. Print the reversed number.
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com