## Python program to check if a number is a perfect number or not:

In this post, we will learn how to check if a number is a *perfect number* or not in Python. I will show you different ways to write this program in Python. The program will take one number as input from the user and print a message if it is a *perfect number* or not.

But, before I start to write the program, let me quickly show you what is a *perfect number* and the algorithm to check for perfect numbers.

### What is a perfect number:

A number is called a *perfect number* if the sum of the divisors of the number is equal to the number itself. For example, *28* is a perfect number, because the sum of its divisors *1, 2, 4, 7, 14* is equal to *28*.

`1 + 2 + 4 + 7 + 14 = 28`

So, we have to find all the divisors of a number, find the sum of all divisors and compare it with the original number to check if a number is a *perfect number* or not.

### Algorithm to check if a number is a perfect number or not:

We can use the below algorithm to check for a perfect number:

- Take the number as input from the user and store it in a variable.
- Initialize one variable as
*0*to hold the sum of its divisors. -
Run one loop from

*1*to*number - 1*and for each value of the loop, check if it is a divisor of the number or not.- If it is a divisor, add it to the sum variable.

- Once the loop ends, compare the
*sum variable*with the original number. If both are equal, it will be a*perfect number*. Else, not.

We can also run the loop from *1* to *number/2* as no value greater than *number/2* can divide the number. It will reduce the loop iterations.

### Method 1: Python program to check for a perfect number:

The below python program checks if a number is a *perfect number* or not:

```
no = int(input("Enter the number: "))
sum = 0
for i in range(1, no):
if no % i == 0:
sum += i
if sum == no:
print(f'{no} is a perfect number.')
else:
print(f'{no} is not a perfect number.')
```

Here,

- The program asks the user to enter a number. It assigns the number to the
*no*variable. - The
*sum*variable is used to hold the sum of all divisors. It is initialized as*0*. - The for loop runs from
*i = 1*to*i = no*. On each iteration of the loop, it checks if*i*can divide*no*or not. If yes, it adds its value to the*sum*variable. - Once the
*for loop*ends, it compares the values of*sum*and*no*. If both are equal, it will be a perfect number.

If you run this program, it will print output as below:

```
Enter the number: 496
496 is a perfect number.
Enter the number: 8128
8128 is a perfect number.
Enter the number: 8129
8129 is not a perfect number.
```

### Method 2: By iterating from 1 to number/2:

We can also iterate from *1* to *number/2*. As I explained above, it will reduce the number of steps of the loop.

Let me change the above program:

```
no = int(input("Enter the number: "))
sum = 0
for i in range(1, int(no/2)+1):
if no % i == 0:
sum += i
if sum == no:
print(f'{no} is a perfect number.')
else:
print(f'{no} is not a perfect number.')
```

Since the type of *no/2* is float, we have to cast it to an integer. It will run from *i = 1* to *i = no/2*.

If you run this program, it will print similar result.

```
Enter the number: 8128
8128 is a perfect number.
```

### Method 3: By using while loops:

We can also use a *while loop* to write the same program. It will run from *number/2* to *1* and find out the sum of all divisors.

```
no = int(input("Enter the number: "))
sum = 0
i = int(no/2)
while i > 0:
if no % i == 0:
sum += i
i = i - 1
if sum == no:
print(f'{no} is a perfect number.')
else:
print(f'{no} is not a perfect number.')
```

- We initialized a variable
*i*as*no/2*. The while loop starts at*i = no/2*. - On each iteration of the loop, it checks if the number is divisible by the current value of
*i*or not. If yes, it adds that value to the*sum*variable. - At the end of each iteration, it decrements the value of
*i*by*1*. - This loop stops when the value of
*i*becomes*0*. - It compares the values of
*sum*and*no*once the loop ends to find if the given number is a perfect number or not.

It will give similar output.

```
Enter the number: 488
488 is not a perfect number.
Enter the number: 496
496 is a perfect number.
```

### Method 4: By using a separate method:

Letâ€™s use a separate method to write the above program. This method will take one number as its parameter and return one *boolean* value. It will return *True* if the number is a perfect number and *False* if it is not. Based on this value, the program will print if the given number is a perfect number or not.

```
def is_perfect(n):
sum = 0
for i in range(1, int(n/2)+1):
if n % i == 0:
sum += i
return sum == n
no = int(input("Enter the number: "))
if is_perfect(no):
print(f'{no} is a perfect number.')
else:
print(f'{no} is not a perfect number.')
```

Here,

- The
*is_perfect*method checks if a number is a perfect number or not. It takes one number as its parameter and returns one boolean value. - The
*if condition*checks the return value of this method and prints one message based on that.

### You might also like:

- Python program to check if a number is a perfect number or not
- 3 different Python programs to remove the first character of string
- Python assert statement explanation with examples
- 3 different Python programs to get a string after a substring
- Python example to print the function name as string with
`__name__`

and`__qualname__`

- Python program to check if a number is a Niven or Harshad number
- 3 ways in Python to calculate GCD or HCF of two numbers
- Python program to print the multiplication table of a specific number