Perfect Number in Python Using While Loop

Perfect Number in Python Using While Loop | We will develop a program to check the perfect number in python using while 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 While Loop

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

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 the number then the number is a perfect number, else it is not a perfect number.

# Python program to check perfect number using while loop

# take inputs
N = 28

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

28 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 while loop

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

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

Enter a number: 30
30 is not a perfect number

Perfect Number in Python using While 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 while loop

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

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

Enter a number: 300
300 is not a perfect number

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

Enter a number: 496
496 is a perfect number

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 *