In this post we will understand the logic to find the reverse number of a given number, then we will implement the same in C++ programming language. Let’s understand the logic:
Programming Logic:
- Input a number from the user.
- The
while
loop is used untiln != 0
is false (0). - In each iteration of the loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by 10 times.
- Inside the loop, the reversed number is computed using:
reverse = reverse*10 + remainder;
Program to find reverse number in C++:
#include<iostream> using namespace std; int main() { int n, rev = 0, remainder; cout<<"Input a Number to Reverse and press Enter: "; cin>> n; // Taking Input Number in variable number while (n != 0) { remainder = n % 10; rev = rev * 10 + remainder; n /= 10; } cout<<"Reversed Number is: "<<rev; return 0; }
OUTPUT:
Input a Number to Reverse and press Enter: 345 Reversed Number is: 543