Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Perfect Numbers (See related posts)

// School project

/*
	Bryan Abrams
	Prof Bioano
	Struct Programming TR at 11
	Homework Five (perfect numbers)
	For Description, see Explain();
*/

#include <iostream>
using namespace std;

//-----start prototypes-----//
void Explain();
int GatherUserData(int,int);
int AnalyzeNumber(int);
void PrintResults(int,int,int);
//-----end   prototypes-----//

int main()
{
	int num1, numRet, numRet2;
	char cont = 'Y';

	Explain();

	do {
		cout << "\nPlease, enter an integer: ";
		cin >> num1;

		numRet = AnalyzeNumber(num1);
		numRet2 = GatherUserData(num1,numRet);
		PrintResults(num1,numRet,numRet2);

		cout << "Do you wish to continue? ";
		cin >> cont;
	}while(toupper(cont) == 'Y');

	return 0;
}

void Explain()
{
	cout << "A number is said to be perfect if the sum of its divisors (except for itself)\n"
		 << "is equal to itself. For example 6, is a perfect number because the sum of its\n"
		 << "divisors (1 + 2 + 3) is equal to 6. The number 8 is said to be defiecient\n"
		 << "because the sum of its divisors (1 + 2 + 4) is only 7. The Number 12 is said\n"
		 << "to be abudant because the sum of its divisors (1 + 2 + 3 + 4 + 5 + 6) is 16.\n\n";
	return;
}

int GatherUserData(int num1, int num2)
{
	if(num2 > num1)
		return 3; // abudant
	else if(num2 < num1)
		return 2; // deficient
	else
		return 1; // perfect
}

int AnalyzeNumber(int num)
{
	int numberTot = 0;
	int numberReturn = 0;

	for(int i = 1; i <= num; i++)
	{
		numberTot = num % i;
		if((numberTot == 0) && (i != num))
		{
			numberReturn += i;
		}
	}

	return numberReturn;
}

void PrintResults(int num1, int retNum, int retNum2)
{
	cout << "The sum of the factors of " << num1 << " is " << retNum << endl;
	if(retNum2 == 1)
		cout << num1 << " is a perfect number." << endl;
	else if(retNum2 == 2)
		cout << num1 << " is a deficient number." << endl;
	else if(retNum2 == 3)
		cout << num1 << " is a abundant number." << endl;

	return;
}



You need to create an account or log in to post comments to this site.


Click here to browse all 4857 code snippets

Related Posts