C++ Program to check number is armstrong or not using class and object


In this program, You will learn how to check number is armstrong or not using class and object in C++.


Some list of armstrong number is : 153, 370, 371


Example: How to check number is armstrong or not using class and object in C++

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

class Test {
public:

    int checkArmstrong(int x) {
        int r, num = 0;

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

int main() {

    int x, arm;

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

    Test obj;
    arm = obj.checkArmstrong(x);

    if (arm == x) {
        cout << "\nNumber is armstrong : " << x;
    } else {
        cout << "\nNumber is not armstrong : " << x;
    }

    return 0;
}

Output:

Enter a Number : 153
Number is armstrong : 153