-
Notifications
You must be signed in to change notification settings - Fork 368
/
perfect.cpp
39 lines (32 loc) · 957 Bytes
/
perfect.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
// C++ program to check if the number is a Perfect number or not
#include<iostream>
using namespace std;
int main ()
{
// initialize variable for loop, input number and to store sum
int i, num, sum=0;
// take user input
cout << "Enter the number: ";
cin >> num;
// iterate from 1 to N and if the number is a factor of N then add it to sum
for (i=1; i < num; i++)
{
int div = num % i;
if (div == 0)
sum = sum + i;
}
// if sum of factors of N is equal to N then it is a perfect number
if (sum == num)
cout << "\n" << num <<" is a perfect number";
else
cout << "\n" << num <<" is not a perfect number";
return 0;
}
/*******************************************************
Case 1:
Enter the number: 153
153 is not a perfect number
Case 2:
Enter the number: 496
496 is a perfect number
*******************************************************/