If the sum of all factors (excluding itself ) of a number is equal to itself, then the number is said to be perfect. For example, 6.

## Steps to Check Perfect Number in Python

1. Input a number.
2. Sum all its factors (excluding itself).
3. Check if the sum is equal to the input number.
4. If yes, then it is a perfect number otherwise, not.

## Check Perfect Number in Python

``````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("Perfect Number")
else:
print("Not a Perfect Number")
``````

Output:

Enter a number
6
Perfect Number

## Find all Perfect Numbers in the Range

``````def isPerfect(num):
sumOfFactors = 0

#Calculating the sum of Factors
for i in range(1,num):
if num%i == 0:
sumOfFactors += i

return sumOfFactors == num

lower = int(input("Enter lower interval: "))
upper = int(input("Enter upper interval: "))

print("Perfect Numbers between {x} and {y}:".format(x=lower,y=upper))
for i in range(lower,upper):
if isPerfect(i):
print(i)``````

Output:

Enter lower interval: 1
Enter upper interval: 100
Perfect Numbers between 1 and 100:
6
28