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

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

    return 0;
}

Output:

Enter a number:123
After reverse number is:321