**Perfect Number Program in Python** | Here, we will develop how to check the perfect number program in Python. **A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself**. For example, we will take **6** as a number and its **divisors are 1, 2, and 3** excluding itself so the sum of its divisors i.e., **1+2+3 = 6**.

## Perfect Number In Python Using For Loop

Here, we use the for loop to find the perfect number by taking user input.

**Step 1:** We take variable N to input the number from the user.**Step 2:** Then we declare the sum to 0.**Step 3:** We use for loop to repeat the range**Step 4:** then in the if loop we find whether the number is divisible by “i” or not then we add the numbers and store them in sum.**Step 5:** if the sum is equal to a number then the number is a perfect number, else it is not a perfect number.

```
# Python program to check perfect number
# take inputs
N = int(input("Enter a number: "))
# check perfect number
sum = 0
for i in range(1,N):
if(N%i == 0):
sum = sum+i
# display result
if(sum == N):
print(N, "is a perfect number")
else:
print(N, "is not a perfect number")
```

Output for the input values test-case-1:-

Enter a number: 6

6 is a perfect number

Output for the input values test-case-2:-

Enter a number: 3

3 is not a perfect number

## Perfect Number In Python Using While Loop

In the previous program, we will use the For Loop to check the perfect number in python. In this program, we use the While Loop to check the perfect number by taking user input.

**Step 1:** We take variable N to input the number from the user.**Step 2:** Then we declare the sum to 0.**Step 3:** We use a while loop to find whether i is lesser than N or not**Step 4:** then in the if loop we find whether the number is divisible by “i” or not then we add the numbers and store them in sum.**Step 5:** if the sum is equal to a number then the number is a perfect number, else it is not a perfect number.

```
# Python program to check perfect number
# take inputs
N = int(input("Enter a number: "))
# check perfect number
i = 1
sum=0
while(i<N):
if(N%i==0):
sum=sum+i
i=i+1
# display result
if(sum == N):
print(N, "is a perfect number")
else:
print(N, "is not a perfect number")
```

Output for the input values test-case-1:-

Enter a number: 28

28 is a perfect number

Output for the input values test-case-2:-

Enter a number: 5

5 is not a perfect number

## Perfect Number in Python using Function

Here, we can also take the help of a function to check the perfect number in python. A function is a block of code that performs a specific task.

**Step 1:** We first define a function perfect_numbers, here we use for loop to find the range and then use the if loop to find where the number is divisible by ‘i’ or not, then we add the i.**Step 2:** Take variable N to input the number from the user.**Step 3:** Call perfect_numbers function**Step 4:** Then if the number is a perfect number print “ ’number’ is a perfect number”, else print “ ‘ number’ is not a perfect number”.

```
# Python program to check perfect number
def perfect_numbers(N):
sum = 0
for i in range(1,N):
if(N%i == 0):
sum = sum+i
return sum
# take inputs
N = int(input("Enter a number: "))
# check perfect number
if(N == perfect_numbers(N)):
print(N, "is a perfect number")
else:
print(N, "is not a perfect number")
```

Output for the input values test-case-1:-

Enter a number: 496

496 is a perfect number

Output for the input values test-case-2:-

Enter a number: 9

9 is not a perfect number

## Perfect Number Program in Python

In this program, we will develop a program to print perfect numbers from 1 to 100 in Python. We will take two ranges while declaring the variables. Then, call the function and print all perfect numbers between the given range.

```
# Python program to print perfect numbers from 1 to 100
def perfect_Number(n): #user-defined function
if n < 1:
return False
perfect_sum = 0
for i in range(1,n):
if n%i==0:
perfect_sum += i
return perfect_sum == n
# take inputs
min_value = 1
max_value = 100
# calling function and print perfect numbers
print('Perfect numbers from %d to %d are:' %(min_value, max_value))
for i in range(min_value, max_value+1):
if perfect_Number(i):
print(i, end=', ')
```

Output:-

Perfect numbers from 1 to 100 are:

6, 28,

## Python Program to Print Perfect Numbers in Given Range

In the previous program, ranges are hardcoded in the program but in this program, ranges will be provided by the user.

```
# Python program to print perfect numbers in an interval
def perfect_Number(n): #user-defined function
if n < 1:
return False
perfect_sum = 0
for i in range(1,n):
if n%i==0:
perfect_sum += i
return perfect_sum == n
# take inputs
min_value = int(input('Print minimum value: '))
max_value = int(input('Print maximum value: '))
# calling function and print perfect numbers
print('Perfect numbers from %d to %d are:' %(min_value, max_value))
for i in range(min_value, max_value+1):
if perfect_Number(i):
print(i, end=', ')
```

Output for the input values test-case-1:-

Print minimum value: 1

Print maximum value: 500

Perfect numbers from 100 to 500 are:

6, 28, 496,

Also See:-

- Perfect Number Using While Loop
- Perfect Number Using For Loop
- Print Perfect Numbers from 1 to 100
- Perfect Number Using Function

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!