C++ program to check the number is Armstrong number or not using class

Given a number, we have to check whether it is an Armstrong number or not using the class and object approach.
Submitted by Shubh Pachori, on August 06, 2022

What is an Armstrong number?

If the sum of its digits is raised to the power number of digits gives the number itself. For example, three-digit Armstrong numbers are 153, 370, 371, 407, and four-digit Armstrong numbers are:1634, 8208, 9474, and there are many more.



153 is an Armstrong number

C++ code to check the number is Armstrong number or not using class and object approach

#include <iostream>
using namespace std;

// create a class
class Armstrong {
  // private data member
  int number;

  // public function with an int type parameter
  int armstrong(int n) {
    // copying the value of the parameter
    // into data members
    number = n;

    // declaring int type variables for indexing
    // and storing result
    int index, remain, result = 0;

    // while loop for manipulating the number
    while (number) {
      // remain contains the last element of the number
      remain = number % 10;

      // result contains the cube of remain added to it
      result = result + (remain * remain * remain);

      // number is divided by 10 at each iteration
      // to get new number
      number = number / 10;

    // returning the result
    return result;

int main() {
  // create a object
  Armstrong A;

  // declaring int type variables
  int n, result;

  cout << "Enter Number: ";
  cin >> n;

  // object is calling the function to get the number
  result = A.armstrong(n);

  // checking if the original number is equal to
  // the manipulated number
  // if it is equal then it will print that number
  // is a Armstrong number
  if (result == n) {
    cout << n << " is an Armstrong number." << endl;

  // else it will print it is not a Armstrong number
  else {
    cout << n << " is not an Armstrong number." << endl;

  return 0;


RUN 1:
Enter Number: 153
153 is an Armstrong number.

RUN 2:
Enter Number: 369
369 is not an Armstrong number.


In the above code, we have created a class Armstrong, one int type data member number to store the number, and a public member function armstrong() to check the given integer number.

In the main() function, we are creating an object A of class Armstrong, reading an integer number by the user, and finally calling the armstrong() member function to check the given integer number. The armstrong() function contains the logic to check the given number whether it is an Armstrong number or not and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.