This post shows how to check whether a number is a perfect number in Python. A perfect number is a positive integer that equals the sum of its proper divisors (divisors excluding itself) — like 6, whose divisors 1, 2, and 3 add up to 6.
Check perfect number in Python:
Explanation:
We loop through every number from 1 up to (but not including) the number itself, add up every number that divides it evenly, and compare that sum to the original number.
Python Program to Check Perfect Number
num = int(input("Enter a number: "))
total = 0
for i in range(1, num):
if num % i == 0:
total += i
if total == num:
print(f"{num} is a perfect number")
else:
print(f"{num} is not a perfect number")
Output:
Enter a number: 28
28 is a perfect number
Divisor and number-theory problems like this are common early interview warm-ups — see more in our Interview Prep section.
