Perfect Number in Python Using For Loop

Perfect Number in Python using For Loop | We will develop a program to check the perfect number in python using for loop. 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.

Python Program To Check Perfect Number Using For Loop

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 take variable N to the input number.
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 using for loop

# take inputs
N = 6

# 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:-

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 for loop

# 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: 28
28 is a perfect number

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

Enter a number: 36
36 is not a perfect number

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

Enter a number: 50
50 is not a perfect number

Perfect Number In Python Using For Loop

We can also 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 for loop

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: 250
250 is not a perfect number

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

Enter a number: 496
496 is a perfect number

Python Program to Print All Perfect Numbers in Given Range

In this program, 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 for loop

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

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 *