This is a C++ Program to Reverse a Given Number Without Using Recursion.
The program takes a given number and prints its reverse.
1. The program takes an integer number.
2. Using a while loop, the digits of the number are reversed.
3. The reverse number is printed.
Here is the source code of C++ Program to Reverse a Given Number Without Using Recursion. The program output is shown below.
using namespace std;
int main ()
int num, temp, rev = 0;
cout << "Enter a number : ";
cin >> num;
temp = num;
while (temp != 0)
r = temp % 10;
rev = rev * 10 + r;
temp /= 10;
cout << "\nThe reverse of " << num << " is : " << rev;
1. The user is asked to enter a number and it is stored in the variable ‘num’.
2. num is copied to a temporary variable ‘temp’. The variable ‘rev’ is initialized as 0.
3. Using a while loop, temp is divided by 10 and the remainder is stored in the variable ‘r’.
4. rev is multiplied by 10 and r is added to it.
5. The loop continues till temp becomes 0.
6. The result is then printed which is the reverse number.
Case 1 : Enter a number : 123 The reverse of 123 is : 321 Case 2 : Enter a number : 58496 The reverse of 58496 is : 69485 Case 3 : Enter a number : 21 The reverse of 21 is : 12
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.