-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPerfectNumber.cpp
68 lines (43 loc) · 862 Bytes
/
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
64
65
66
67
#include<iostream>
using namespace std;
/*Program to find PERFECT NUMBER- a perfect number is a number whose factors leaving itself when added equals to the number itself.
eg-6, its factors= 1,2,3 = 3+2+1=6 , 28 , factors(28) = 1,2,4,7,14 = 28
to check if a number is a perfect number or not
*/
bool PerfectNumber( long int n)
{
long int factors=1;
long int sum = 0;
while(n > factors)
{
//if n is divisible by factors
if(n%factors==0)
{
sum += factors;
}
//otherwise simply increment and again check
factors++;
}
if(sum==n)
{
return true;
}
return false;
}
//function to find all the Perfect numbers till a range
void PerfectNumbersInRange(int range)
{
int i=1;
while(i<=range)
{
if(PerfectNumber(i))
{
cout<<i<<endl;
}
i++;
}
}
int main()
{
PerfectNumbersInRange(10000);
}