In this post, we will write a python program to check a perfect number but before writing the program let’s understand what is a perfect number.

## What is the Perfect Number?

A number that is equal to the sum of its proper divisors is known as a perfect number.

for example: **6** —-> proper divisor of 6 is 1,2,3 and sum of 1+2+3 = 6, that means 6 is perfect number.

we should write this program in three different ways:

- simple (by taking user input).
- by using function.
- except error (using try-except) if the user input wrong data.

Now let’s write program to check perfect number.

## 1. Simple (by taking user input)

for this program you should have knowledge about:

**Source code**:

```
number = int(input("Enter A number :"))
sum = 0
print(f"divisor of {number} is : ",end="")
for i in range(1,number):
if number % i == 0:
print(i,end=",")
sum+=i
print("\n")
if sum == number:
print(f"{number} is perfect number")
else:
print(f"{number} is not perfect number")
```

**Output-1:**

```
Enter A number :6
divisor of 6 is : 1,2,3,
6 is perfect number
```

**Output-2:**

```
Enter A number :14
divisor of 14 is : 1,2,7,
14 is not perfect number
```

## Explaination

** number** variable is used to take integer value from the user.

** sum** variable (which is initially zero) is used to store the sum of divisors.

then after we find all the divisors using the **for-loop & if statement** and store the sum of all the divisors of the ** number** in

**the variable.**

`sum`

after that, ** if sum == number** then the

**is a perfect number otherwise it is not a perfect number.**

`number`

## 2. By using function

Write a python program to check the perfect number using a function.

For this program you should have Knowledge about:

**Source code:**

```
def Perfect_number(num):
sum = 0
print(f"divisor of {number} is : ",end="")
for i in range(1,num):
if num % i == 0:
print(i,end=",")
sum+=i
print("\n")
if sum == num:
return f"{num} is perfect number"
else:
return f"{num} is not perfect number"
number = int(input("Enter A number :"))
print(Perfect_number(number))
```

**Output-1:**

```
Enter A number :28
divisor of 28 is : 1,2,4,7,14,
28 is perfect number
```

**Output-2:**

```
Enter A number :36
divisor of 36 is : 1,2,3,4,6,9,12,18,
36 is not perfect number
```

## 3. except error (using try-except) if user input wrong data.

Write a program to check the perfect number in which except error (using try-except) if the user input wrong data.

For this program you should have Knowledge about:

Now let’s write a program and see the output and how it’s working.

**Source code:**

```
def Perfect_number(num):
sum = 0
print(f"divisor of {number} is : ",end="")
for i in range(1,num):
if num % i == 0:
print(i,end=",")
sum+=i
print("\n")
if sum == num:
return f"{num} is perfect number"
else:
return f"{num} is not perfect number"
while True:
try:
number = int(input("Enter A number :"))
if type(number) == int:
break
except ValueError:
print("pls Enter valid number(only numberical data allowed)")
print(Perfect_number(number))
```

**Output-1:**

```
Enter A number :six
pls Enter valid number(only numberical data allowed)
Enter A number :6
divisor of 6 is : 1,2,3,
6 is perfect number
```

**Output-2:**

```
Enter A number :sixteen
pls Enter valid number(only numberical data allowed)
Enter A number :16
divisor of 16 is : 1,2,4,8,
16 is not perfect number
```

Hope this content adds some value to your life -thank you.