Python Program to Check Perfect Number

Last Updated:

What is Perfect Number?

A positive integer that is equal to the sum of its proper divisors.

For example, 6 is a perfect number
6 = 1 + 2 + 3
6 = 6

Tips: It is recommended to use our online Perfect Number calculator for better understanding.

Check Perfect Number

In the following example, we will check whether the given number (496) is a Perfect number or not.

Example

Python Compiler
num = 496 total = 0 for i in range(1, num): if(num % i == 0): total += i if(total == num): print("%d is a perfect number" % num) else: print("%d is not a perfect number" % num)

Perfect Numbers between the Given Range

In the following example, we will find all the Perfect numbers between 1 and 1000.

Example

Python Compiler
start = 1 end = 1000 flag = 0 print("Prefect numbers between %d and %d: " %(start, end)) for start in range(start, end): total = 0 for i in range(1, start): if(start % i == 0): total += i if((total == start) and (start != 0)): if(flag == 0): flag = 1 print(start, end=" ") if(flag == 0): print("There is no perfect numbers between the given range")

Check Whether the Given Number is Perfect or Not

In the following example, we will check whether the given number is a Perfect Number or Not.

Example

Python Compiler
num = int(input("Enter a number: ")) total = 0 for i in range(1, num): if(num % i == 0): total += i if(total == num): print("%d is a perfect number" % num) else: print("%d is not a perfect number" % num)

Share this Page

Meet the Author