Python Program to find Perfect Number

How to write a Python program to find Perfect Number using For Loop, While Loop, and Functions.

Python Perfect Number

Any number can be perfect number in Python, if the sum of its positive divisors excluding the number itself is equal to that number.

For example, 6 is a perfect number in Python because 6 is divisible by 1, 2, 3 and 6. So, the sum of these values are: 1+2+3 = 6 (Remember, we have to exclude the number itself. That’s why we haven’t added 6 here). Some of the perfect numbers are 6, 28, 496, 8128 and 33550336 so on.

Python Program to find Perfect Number using For loop

This Python program for Perfect Number allows the user to enter any number. Using this number it will calculate whether the number is Perfect number or not using the Python For Loop.

# Python Program to find Perfect Number using For loop

Number = int(input(" Please Enter any 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)

Within this Python perfect number program, the following statement will ask the user to enter any number and stores the user entered value in variable Number

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

Next line, We declared integer variable Sum = 0. Next, we used the for loop with range() and we are not going to explain the for loop here. If you don’t understand for loop then please visit our article Python For Loop.

for i in range(1, Number):

Inside the For loop we placed Python If Statement to test the condition

if(Number % i == 0):
        Sum = Sum + i

If statement inside the for loop will check whether the Number is Perfectly divisible by i value or not. If the Number is perfectly divisible by i then i value will be added to the Sum. Please refer Python If Statement to article to understand the If statement in Python.

Python Program to find Perfect Number using For loop

From the above Python perfect number example, User Entered value is: Number = 6

First Iteration: For the first Iteration, Number = 6, Sum = 0 and i = 1

If (Number % i == 0)
6 % 1 == 0

If statement is succeeded here So,

Sum = Sum + i
Sum = 0 +1 = 1

Second Iteration: For the second Iteration the values of both Sum and i has been changed as: Sum = 1 and i = 2

If (Number % i == 0)
6 % 2 == 0

If statement is succeeded here So,

Sum = Sum + i
Sum = 1 + 2 = 3

Third Iteration: For the third Iteration o this python perfect number program, the values of both Sum and i has been changed as: Sum = 3 and i = 3

If (Number % i == 0)
6 % 3 == 0

If statement is succeeded here So,

Sum = Sum + i
Sum = 3 + 3 = 6

For the fourth and fifth iterations condition inside the if condition will fail

6 % 4 == 0 (FALSE)

6 % 5 == 0 (FALSE)

So, compiler will terminate the for loop.

In the next line we have If statement to check whether the value inside the Sum variable is exactly equal to given Number or Not.

If the condition (Sum == Number) is TRUE then below print statement will execute

print(" %d is a Perfect Number" %Number)

If the condition (Sum == Number) is FALSE then below print statement will be executed

print(" %d is not a Perfect Number" %Number)

For this example (Sum == Number) is True. So, given Number is Perfect Number

Python Program to find Perfect Number using While Loop

This Python perfect number program allows the user to enter any number. Using this number it will calculate whether the user input is Perfect number or not using Python While loop.

# Python Program to find Perfect Number using While loop

Number = int(input(" Please Enter any Number: "))
i = 1
Sum = 0
while(i < Number):
    if(Number % i == 0):
        Sum = Sum + i
    i = i + 1
if (Sum == Number):
    print(" %d is a Perfect Number" %Number)
else:
    print(" %d is not the Perfect Number" %Number)

We haven’t done anything special in this example. We just replaced the For loop in the above python program with While Loop and if you find difficult to understand the While loop functionality then please refer Python While Loop article.

 Please Enter any Number: 496
 496 is a Perfect Number
>>> 
 Please Enter any Number: 525
 525 is not the Perfect Number

Python Program to find Perfect Number using Function

This program for Perfect number allows the user to enter any number. Using this number it will calculate whether the number is Perfect number or not using the Functions.

# Python Program to find Perfect Number using Functions

def Perfect_Number(Number):
    Sum = 0
    for i in range(1, Number):
        if(Number % i == 0):
            Sum = Sum + i
    return Sum        

# Taking input from the user
Number = int(input("Please Enter any number: "))
if (Number == Perfect_Number(Number)):
    print("\n %d is a Perfect Number" %Number)
else:
    print("\n %d is not a Perfect Number" %Number)

If you observe the above code, We haven’t done anything special in this example. We just defined the new user defined function and added the code we mentioned in For loop example.

Please Enter any number: 33550336

 33550336 is a Perfect Number
>>> 
Please Enter any number: 54

 54 is not a Perfect Number

Python Program to Find Perfect Number between 1 to 1000

This Perfect number program allows the user to enter minimum and maximum values. Next, this Python program will find the Perfect Number between the Minimum and Maximum values.

# Python Program to find Perfect Number between 1 to 1000

# Taking input from the user
Minimum = int(input("Please Enter any Minimum Value: "))
Maximum = int(input("Please Enter any Maximum Value: "))

# initialise sum


# Checking the Perfect Number
for Number in range(Minimum, Maximum - 1):
    Sum = 0
    for n in range(1, Number - 1):
        if(Number % n == 0):
            Sum = Sum + n       
    # display the result
    if(Sum == Number):
        print(" %d " %Number)

Python Perfect Numbers from 1 to 100 and 1 to 1000 output

Please Enter any Minimum Value: 1
Please Enter any Maximum Value: 100
 6 
 28 
>>> 
Please Enter any Minimum Value: 1
Please Enter any Maximum Value: 1000
 6 
 28 
 496 

Within this perfect number program, this For Loop helps compiler to iterate between Minimum and Maximum Variables, iteration starts at the Minimum and then it will not exceed Maximum variable.

for Number in range(Minimum, Maximum):

Inside the for loop we are checking whether that number is perfect number or not. We already explained the for loop iteration in the first example.