C++ Program to reverse a number


In this program, You will learn how to reverse a number in C++.


123 => 321

153 => 351


Example: How to reverse a number in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int n, rev, r;
    cout << "Enter a Number :";
    cin>>n;

    rev = 0;
    while (n > 0) {
        r = n % 10;
        rev = rev * 10 + r;
        n = n / 10;
    }
    cout << "\nThe Reverse Number is :" << rev;

    return 0;
}

Output:

Enter a number :123
The Reverse Number is :321