Creating a Program to compute Abundant numbers and Perfect numbers c++

  c++, c++11

I’m attempting to make a program that computes abundant numbers and perfect numbers. I am supposed to use bool functions as well as void, however, I am unsure of how to use the bool for this.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//Function Prototypes
void getMenu();
void getValidUserInputPosNumGT0(int &number);
int isAbundant(int num);
int isPerfect(int num);
int main() {
//Constant choices for menu
const int ABUNDANT = 1; // Accesses count abundant menu option
const int PERFECT = 2; // Accesses compute perfect of number menu option
const int QUIT_CHOICE = 3; // Quits program
int choice; //Menu option selected by user
int number; // Number provided by user
do {
// Display the menu and get the user's choice.
    getMenu();
    cin >> choice;
//Validate user input
    while (choice < ABUNDANT || choice > QUIT_CHOICE)
    {
        cout << "Please enter a valid menu choice: ";
        cin >> choice;
    }
    if (choice == ABUNDANT)
    {
        getValidUserInputPosNumGT0(number);
        isAbundant(number);
    }
    else if (choice == PERFECT)
    {
        getValidUserInputPosNumGT0(number);
        cout<<"The factorial of the number is: "<<isPerfect(number)<<"n";
    }
}
while (choice != QUIT_CHOICE);
return 0;
}
//*****************************************************************
// Definition of function getMenu which displays the welcome menu.
//*****************************************************************
void getMenu()
{
cout << "Welcome to the Playing With Numbers program! " << endl;
cout << "1) Compute if the number is abundantn ";
cout << "2) Compute if the number is a perfect numbern ";
cout << "3) Quit " << endl;
cout << "Select an option (1,2,3) " << endl;
}
//*****************************************************************
// Definition of function getValidUserInputPosNumGT0 that allows *
// user to enter and validates positive integer *
//*****************************************************************
void getValidUserInputPosNumGT0(int &num)
{
cout << "Enter in a positive number greater than 0 .. " << endl;
cin >> num;
// Start the while loop
while (num < 0)
{
    cout << "Please try again .." << endl;
    cout << "Enter in a positive number greater than 0 .. " << endl;
    cin >> num;
}
}
//*****************************************************************
//Definition of isAbundant function allows user to enter a *
//*****************************************************************
int isAbundant(int n) {
long int sum = 1;


for (long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
        if (i * i != n) {
            sum = sum + i + n / i;
            cout << n << "is abundant.n";
        }
        else
            sum = sum + i;
            cout << n << "is not abundantn";

    }
    }


    return 0;
    }
    //*****************************************************************
    //Definition of isPerfect function allows user to enter a *
    //*****************************************************************
    int isPerfect(int num)
    {
    // Check the condition.
    if (num == 0)
    return 1;
    else
    // Call the function.
    return num * isPerfect(num -1);
    }

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.
therefore the terminal will output

6 is a perfect number

An abundant number is a number for which the sum of its proper divisors is greater than the number. The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4, and 6 for a total of 16. The amount by which the sum exceeds the number is the abundance. The number 12 has an abundance of 4, for example. Therefore, if the input is 12, the terminal will output
12 is an abundant number.

Source: Windows Questions C++

LEAVE A COMMENT