Skip to content

anonymous /gist:3921907
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.