Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- bool isAFactor(int a, int b)
- {
- return ((a % b) == 0);
- }
- int main()
- {
- int startval, endval;
- cout << "This program will find all perfect numbers given a range of number.\n";
- cout << "Please select a start value: " ;
- cin >> startval;
- cout << "Please select an end value: " ;
- cin >> endval;
- if (endval < startval)
- {
- cout << "Error: The end value must be greater than the start value.\n";
- return 0;
- }
- int runningTotal = 0;
- bool found = false;
- for (int i = startval; i <= endval; i++)
- {
- for(int j = 1; j <= i / 2; j++)
- {
- if (isAFactor(i, j))
- {
- runningTotal += j;
- }
- }
- if (runningTotal == i)
- {
- cout << "Perfect Number found : " << runningTotal << "\n";
- found = true;
- }
- runningTotal = 0;
- }
- if (found == false)
- {
- cout << "There is no perfect number between " << startval << " and " << endval << ".\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement