-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathPerfect_Number.cpp
48 lines (40 loc) · 1020 Bytes
/
Perfect_Number.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
/*
Perfect number is a positive integer which is equal to the sum of its proper positive divisors.
For example: 6 is the first perfect number
Proper divisors of 6 are 1, 2, 3
Sum of its proper divisors = 1 + 2 + 3 = 6.
Hence 6 is a perfect number.
*/
#include <bits/stdc++.h>
using namespace std;
int perfectnumber(int n)
{
int sum=0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
sum += i;
}
return sum;
}
int main()
{
int n;
cout<<"Enter a number to check whether it is a perfect number or not: ";
cin >> n;
if (perfectnumber(n) == n)
cout << n << " is a perfect number." << endl;
else
cout << n << " is not a perfect number." << endl;
return 0;
}
/*
Time Complexity: O(n)
Space Complexity: O(1)
sample 1:
Input: Enter a number to check whether it is a perfect number or not: 6
Output: 6 is a perfect number.
sample 2:
Input: Enter a number to check whether it is a perfect number or not: 7
output: 7 is not a perfect number.
*/