-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforperfectnum.py
More file actions
62 lines (47 loc) · 770 Bytes
/
forperfectnum.py
File metadata and controls
62 lines (47 loc) · 770 Bytes
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
# check if given number is perfect or not
n=int(input('enter a number:'))
sum=0
for num in range(1,n):
if n%num==0:
sum+=num
if sum==n:
print(f'{n} is a perfect number')
'''
n=6:
Step 1: Input
User inputs
n=6.
Initialize
sum=0.
Step 2: Start for loop with
num ranging from 1 to
n−1 (i.e., 1 to 5).
Step 3: Iterations inside loop:
num=1:
Check if 6%1==0 (True).
Add
1 to
sum → sum=0+1=1.
num=2:
Check if
6%2==0 (True).
Add 2 to sum →sum=1+2=3.
num=3:
Check if 6%3==0 (True).
Add
3 to sum → sum=3+3=6.
num=4:
Check if
6%4==0 (False).
sum remains 6.
num=5:
Check if 6%5==0 (False).
sum remains
6.
Step 4: Loop ends after
num=5.
Step 5: Check if sum==n:
sum=6,
n=6, so condition is True.
Print "6 is a perfect number".
'''