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

Given a number, we have to check whether it is abundant number or not using class and object approach.
Submitted by Shubh Pachori, on October 04, 2022

Example:

Input:
Enter Number : 30

Output:
It is an Abundant Number

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

#include <iostream>
using namespace std;

// create a class
class Abundant {
  // private data member
 private:
  int number;

  // public member functions
 public:
  // getNumber() function to insert number
  void getNumber() {
    cout << "Enter Number : ";
    cin >> number;
  }

  // isAbundant() function to check if the number
  // is abundant is or not
  void isAbundant() {
    // initialising  int type variables to perform operations
    int index, sum = 0;

    // for loop to check the number
    for (index = 1; index < number; index++) {
      // if condition to check if the number
      // is a multiple of the index
      if (number % index == 0) {
        sum = sum + index;
      }
    }

    if (sum > number) {
      cout << "It is an Abundant Number";
    } else {
      cout << "It is not an Abundant Number";
    }
  }
};

int main() {
  // create an object
  Abundant A;

  // calling getNumber() function to
  // insert the number
  A.getNumber();

  // calling isAbundant() function to check the
  // number if it is a abundant number
  A.isAbundant();

  return 0;
}

Output:

Enter Number : 30
It is an Abundant Number

Explanation:

In the above code, we have created a class Abundant, one int type data member number to store the number, and public member functions getNumber() and isAbundant() to store and to check the given integer number is a abundant number or not.

In the main() function, we are creating an object A of class Abundant, reading an integer number by the user using the getNumber() function, and finally calling the isAbundant() member function to check the given integer number. The isAbundant() function contains the logic to given check the number and printing the result.

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




Comments and Discussions!

Load comments ↻





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