Perfect Number In Python Using Function

Perfect Number In Python Using Function | We will develop a program to check the perfect number in python using function. 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. We can take the help of a function to check the perfect number or not in python. A function is a block of code that performs a specific task.

Python Program to Check Perfect Number Using Function

This is the simplest and easiest way to check perfect numbers in Python. Here, we use the for loop to find the perfect number.

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 using function

def perfect_numbers(N):  #user-defined function
   sum = 0
   for i in range(1,N): 
      if(N%i == 0):
         sum = sum+i 
   return sum 

# take inputs
N = 6

# check perfect number or not
if(N == perfect_numbers(N)): 
   print(N, "is a perfect number") 
else: 
   print(N, "is not a perfect number") 

Output:-

6 is a perfect number

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

# Python program to check perfect number using function

def perfect_numbers(N):  #user-defined function
   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 or not
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: 5
5 is not a perfect number

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

Enter a number: 28
28 is a perfect number

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

Enter a number: 75
75 is not a perfect number

Perfect Number in Python Using Function

This python program also performs the same task but in different ways. In the previous program, we used the for loop but in this program, we use the while loop to check perfect number or not.

# Python program to check perfect number using function

def perfect_numbers(N):   #user-defined function
   i = 1 
   sum = 0 
   while(i < N): 
     if(N%i == 0): 
       sum = sum+i 
   i=i+1
   return sum

# take inputs
N = int(input("Enter a number: "))

# display result
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: 360
360 is not a perfect number

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

Enter a number: 496
496 is a perfect number

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

Enter a number: 8128
8128 is a perfect number

Python Program to Print Perfect Numbers in Given Range

Here, we will develop a python program to print perfect numbers in an interval. 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 using function

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: 50
Perfect numbers from 1 to 50 are:
6, 28,

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!

Leave a Comment

Your email address will not be published. Required fields are marked *