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

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

Example:

Input:
Enter Number : 18

Output:
It is not a Deficient Number

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

#include <iostream>
using namespace std;

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

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

  // isDeficient() function to check if the number
  // is deficient is or not
  void isDeficient() {
    // 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 < (2 * number)) {
      cout << "It is a Deficient Number";
    } else {
      cout << "It is not a Deficient Number";
    }
  }
};

int main() {
  // create an object
  Deficient D;

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

  // calling isDeficient() function to check the
  // number if it is a deficient number
  D.isDeficient();

  return 0;
}

Output:

Enter Number : 55
It is a Deficient Number

Explanation:

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

In the main() function, we are creating an object D of class Deficient, reading an integer number by the user using the getNumber() function, and finally calling the isDeficient() member function to check the given integer number. The isDeficient() 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.