Home » Check is a number is a Perfect Number in python

Check is a number is a Perfect Number in python

In this example we check if a number is a perfect number using python

In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.

For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.

Example

Number = int(input(" Please Enter a Number: "))

Sum = 0

for i in range(1, Number):
    if(Number % i == 0):
        Sum = Sum + i
        
if (Sum == Number):
    print(" %d is a Perfect Number" %Number)
else:
    print(" %d is not a Perfect Number" %Number)

When you run this you will see something like this

>>> %Run perfectnumber.py
 Please Enter a Number: 6
 6 is a Perfect Number

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More