If the sum of all factors of a number ( exclusive itself ) is equal to the number, then the number is said to be perfect. E.g 6
Steps to Check Perfect Number
- Take a number as input.
- Calculate the sum of all its factors(excluding itself)
- Check if the sum of its factors is equal to the input number.
- If YES then its a perfect number, else it is not.
num = int(input("Enter a number \n"))
sumOfFactors = 0
#Calculating the sum of Factors
for i in range(1,num):
if num%i == 0:
sumOfFactors += i;
if sumOfFactors == num:
print("Not a Perfect Number")
That’s all we need to write in order to check a perfect number in python. If you have any doubt’s then comment below.