-
Notifications
You must be signed in to change notification settings - Fork 3
/
perfectNumber.cpp
63 lines (50 loc) · 1.68 KB
/
perfectNumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;
// Function to check if a number is a perfect number
bool isPerfectNumber(int num) {
int sum = 0;
bool isEqual;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i; // Add the divisor to the sum
}
}
isEqual = (sum==num);
return isEqual; // Check if the sum of divisors is equal to the number
}
int main() {
int number,choice;
bool playagain = true;
cout << "\n\nA perfect number is a positive integer that is equal to the sum of its positive divisors, excluding itself. \nFor example, 28 is a perfect number \nbecause its divisors (1, 2, 4, 7, and 14) add up to 28.";
do{
while(true)
{
cout << "\n\nEnter a number to check if it's a perfect number: ";
cin >> number;
if (number > 0) {
break; // Exit the loop if the number is valid
}
else {
cout << "Please enter a number greater than 0." << endl;
}
}
if (isPerfectNumber(number)) {
cout << number << " is a perfect number." << endl;
} else {
cout << number << " is not a perfect number." << endl;
}
cout <<endl<< "Check another number? \n";
do{
cout << "Press 1 for yes, 2 for No\n";
cin >> choice;
if(choice!=2 && choice!=1){
cout << "Please enter valid response \n\n";
}
}while(choice!=2 && choice!=1);
if(choice==2){
playagain=false;
cout << "BYEE HAVE A GREAT TIME! ";
}
}while(playagain==true);
return 0;
}