Skip to content

Instantly share code, notes, and snippets.

Created October 20, 2012 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3921907 to your computer and use it in GitHub Desktop.
Save anonymous/3921907 to your computer and use it in GitHub Desktop.
Perfect number
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
bool isAFactor (int, int);
int main ()
{
int startVal, endVal, sum = 0;
cout << "Enter starting integer: ";
cin >> startVal;
cout << endl;
cout << "Enter ending integer: ";
cin >> endVal;
cout << endl;
for (int i = startVal; i <= endVal; i++)
{
for (int j = 1; j <= i/2; j++)
{
if (isAFactor(i,j))
sum += j;
}
if (sum == i)
{
cout << i << " is a perfect number." << endl;
}
if (sum!= i)
{
cout << "There is no perfect number in this range." << endl;
}
sum = 0;
}
cout << endl;
system("PAUSE");
return 0;
}
bool isAFactor(int a, int b)
{
if ( a % b == 0)
return true;
else
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment