Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool isAFactor(int a, int b)
  5. {
  6. return ((a % b) == 0);
  7. }
  8.  
  9. int main()
  10. {
  11. int startval, endval;
  12.  
  13. cout << "This program will find all perfect numbers given a range of number.\n";
  14.  
  15. cout << "Please select a start value: " ;
  16. cin >> startval;
  17.  
  18. cout << "Please select an end value: " ;
  19. cin >> endval;
  20.  
  21. if (endval < startval)
  22. {
  23. cout << "Error: The end value must be greater than the start value.\n";
  24. return 0;
  25. }
  26.  
  27. int runningTotal = 0;
  28. bool found = false;
  29.  
  30. for (int i = startval; i <= endval; i++)
  31. {
  32. for(int j = 1; j <= i / 2; j++)
  33. {
  34. if (isAFactor(i, j))
  35. {
  36. runningTotal += j;
  37. }
  38. }
  39.  
  40. if (runningTotal == i)
  41. {
  42. cout << "Perfect Number found : " << runningTotal << "\n";
  43. found = true;
  44. }
  45.  
  46. runningTotal = 0;
  47. }
  48.  
  49. if (found == false)
  50. {
  51. cout << "There is no perfect number between " << startval << " and " << endval << ".\n";
  52. }
  53.  
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement