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++.

#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 << "Reverse number is:" << rev;

    return 0;
}

Output:

Enter a number:123
Reverse number is:321