C++ Program to Reverse Digits of a Number

  • Write a program in C++ 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