Javaexercise.com

Python Program to Find Perfect Number

A perfect number is a number that is equal to the sum of its divisor, excluding the number itself, i.e. if we sum up all the divisors of a number, excluding the number itself will be equal to the number. For example, 6 is a perfect number because its divisors are 1, 2, and 3, and the sum of these is 1 + 2 + 3 = 6. Let's see a python program to find the perfect number.

Algorithm To Find Perfect Number in Python

Step 1: Create two variables number and div_sum, and initialize with the number and 0, respectively.

Step 2: Start a loop and set a condition from 1 to number.

Step 3: Add an if condition to check whether the divisor is successful.

Step 4: Add Divisor to the div_sum variable.

Step 5: Add an if condition to check whether the number and div_sum both are the same.

Step 6: Print It is a Perfect Number if both are the same, It is not Perfect Number otherwise.

*number = 6 in our program case.

Let's see its implementation in Python language.

"""
Python Program to Check Perfect Number

"""
# Take a number
number = 6    
div_sum = 0
for i in range(1,number):
  if number%i==0:
    div_sum += i

if div_sum == number:
  print("It is a Perfect Number")
else:
  print("It is not Perfect Number")

Output:

It is a Perfect Number