Example HTML page

A Python Program to check if a number is a Perfect number.

A perfect number: a number is perfect when the sum of its divisors (except the number itself) equals the given number.

Some of the perfect numbers are 6, 28, 496 and 8128.

6 =>sum of its divisors=> 1 + 2 + 3,
28 => sum of its divisors =>1 + 2 + 4 + 7 + 14,
496 = > sum of its divisors =>1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
8128 => sum of its divisors=> 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064

 Program

x = int(input("Enter any number: "))
sum = 0
for y in range(1, x):
    if(x % y == 0):
        sum = sum + y
if (sum == x):
    print(x, " is a Perfect number!")
else:
    print(x," is not a Perfect number!")

Output

Enter any number: 6

6 is a Perfect number!

 

Enter any number: 11

11 is not a Perfect number!

6 =>sum of its divisors=> 1 + 2 + 3=6

11 =>sum of its divisors=> 1 +11≠ 11

6 is a perfect number because the sum of its divisors (except the number itself) equals the given number i.e., 6. 11 is not a perfect number because the divisors of 11 are 1 and 11. But according to the definition of a perfect number the sum of its divisors (except the number itself) equals the given number. Here the sum of divisors of 11 is not equal to given number i.e., 11.

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest