×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to print Armstrong numbers in a range using class

Submitted by Shubh Pachori, on August 14, 2022

Problem statement

Given a range, we have to print all Armstrong numbers in that range using the class and object approach.

Example:

Input:
Enter Range: 100 200

Output: 
Armstrong Numbers in the range 100 to 200 are 
153

C++ code to print Armstrong numbers in a range using class and object approach

#include <iostream>
using namespace std;

// create a class
class IsArmstrong {
  // declare private data members
 private:
  int start, end;

  // public functions
 public:
  // getRange() function to insert range
  void getRange() {
    cout << "Enter Range: ";
    cin >> start >> end;
  }

  // isArmstrong() function to print armstrong numbers in
  // between range
  void isArmstrong() {
    // initialize int type variable for indexing
    int index;

    cout << "Armstrong Numbers in the range " << start
         << " to " << end << " are " << endl;

    // for loop to search armstrong number in the range
    for (index = start; index <= end; index++) {
      // initilising int type variables to perform
      // operations
      int remain, result = 0;

      // copying value of index in number variable
      int number = index;

      // 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;
      }

      // if the result is equal to the index then it will
      // print it
      if (result == index) {
        cout << index << " ";
      }
    }
  }
};

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

  // calling getRange() function to insert range
  A.getRange();

  // isArmstrong() function search armstrong numbers in the
  // range
  A.isArmstrong();

  return 0;
}

Output

RUN 1:
Enter Range: 1 1000
Armstrong Numbers in the range 1 to 1000 are 
1 153 370 371 407

RUN 2:
Enter Range: 100 1000
Armstrong Numbers in the range 100 to 1000 are 
153 370 371 407

Explanation

In the above code, we have created a class IsArmstrong, two int type data members start and end to store the start and end of the range, and public member functions getRange() and isArmstrong() to store and print Armstrong numbers in between that given range.

In the main() function, we are creating an object A of class IsArmstrong, reading a range by the user using getRange() function, and finally calling the isArmstrong() member function to print the Armstrong numbers in the given range. The isArmstrong() function contains the logic to print Armstrong numbers in the given range and printing the result.

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

Comments and Discussions!

Load comments ↻





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