samedi 9 octobre 2021

Creating a Program to compute Abundant numbers and Perfect numbers c++ [closed]

I'm attempting to make a program that computes abundant numbers and perfect numbers. I am supposed to use void functions.

#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 abundant\n ";
    cout << "2) Compute if the number is a perfect number\n ";
    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 abundant\n";

        }
    }

    return 0;
}

//*****************************************************************
//Definition of isPerfect function allows user to enter a         *
//*****************************************************************
int isPerfect(int num) {
    long int sum = 1;

    // Find all divisors and add them
    for (int i = 1; i <= sqrt(num); i++) {
        if (num % i == 0) {
            // If divisors are equal,take only one
            // of them
            if (num / i == i)
                sum = sum + i;

            else //Otherwise take both
            {
                sum = sum + i;
                sum = sum + (num / i);
            }
        }
    }
}

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.

My question is how to get the code to output the "[number] is an abundant number" or "[number] is not an abundant number" as well as "[number] is a perfect number" and vice versa, depending on what the user decides from the menu.

Aucun commentaire:

Enregistrer un commentaire