In this program, we will check whether a number is a Perfect number or not.
Definition of perfect number: A positive integer number that is equal to the sum of its proper divisors. The smallest perfect number is 6, its divisors are 1, 2, and 3 and their total sum is 6.
Other perfect numbers are 28, 496, 128.
Steps to solve the program
- Take a number as input through the user.
- Create a variable and assign its value equal to 0.
- Use for loop with the range function to find the factors of the given numbers.
- Add those factors to the created variable.
- If the value of the variable is equal to the number then it is a 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("Number is a perfect number")
else:
print("Number is not a perfect number")
Output :
Enter a number: 28
Number is a perfect number