Created
October 20, 2012 03:29
-
-
Save anonymous/3921907 to your computer and use it in GitHub Desktop.
Perfect number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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