C++ Program to Reverse Digits of a Number

Here is a C++ program to reverse the digits of a number.


C++ program to reverse digits of a number

#include <iostream>

using namespace std;
 
int main(){
    int number, reverse = 0, rightDigit;
    
 cout << "Enter a number\n";
    cin >> number;
    
    while(number != 0){
        rightDigit = number % 10;
        reverse = (reverse * 10) + rightDigit;
        number = number/10;
    }
     
    cout << "Reversed Number : " << reverse;
  
    return 0;
}
Output
Enter a number
12345
Reversed Number : 54321

Recommended Posts
C++ Program to Find Power of a Number
C++ Program to Find Sum of Natural Numbers
C++ Program to Find LCM and GCD of Two Numbers
C++ Program to Find Largest of Three Numbers
C++ Program to Generate Random Numbers
C++ Program to Find Factorial of a Number
C++ Program to check Whether a Number is Palindrome or Not
C++ Program to Multiply Two Numbers Using Addition
C++ Program to Find Power of Number using Recursion
All C++ Programs