C++ Program to reverse a number using class and object


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


123 => 321

123 => 321


Example: How to reverse a number using class and object in C++

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

class Test {

public:
    int reverse(int x) {

        int r, rev = 0;
        while (x > 0) {
            r = x % 10;
            rev = rev * 10 + r;
            x = x / 10;
        }
        return rev;
    }
};

int main() {

    int x, rev;

    cout << "Enter a Number :";
    cin >> x;

    Test obj;
    rev = obj.reverse(x);

    cout << "\nAfter Reverse Number is :" << rev;

    return 0;
}

Output:

Enter a Number :456
After Reverse Number is :654