forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfect_number.cpp
68 lines (43 loc) · 1.14 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
A program to check whether the given number is a perfect number or not.
Perfect number is a positive whole number that is equal to the sum of its proper divisors.
The first perfect number is 6 as the sum of its proper positive divisors, 1,2 and 3 is 6.
Other perfect numbers are 28, 496, 8128,etc.
*/
#include<iostream>
using namespace std;
//function to check perfect number
void check_perfect_number(int num)
{
int sum = 0;
int i;
//loop till half the number because further the multiples will be greater than the number itself.
for(i = 1 ; i <= (num/2) ; i++)
{
int rem = num%i;
if(rem == 0)
sum = sum + i;
}
if(num == sum)
cout<<"You entered a perfect number\n";
else
cout<<"Not perfect number\n";
}
//main
int main(){
int number, sum = 0, rem, i;
cout<<"Enter number:\n";
cin>>number;
//function call
check_perfect_number(number);
return 0;
}
/*
Test cases:
1. Input : Enter any number: 6
Output : You entered a perfect number
2.Input: Enter any number: 15
Output: Not perfect number
Time complexity: O(n)
Space Complexity : O(1)
*/