Write A Python Function To Check Whether A Number Is Perfect Or Not

A perfect number is a positive integer that is equal to the sum of its factors, excluding the number.

for example 6

factors of a number 6 is 1, 2, 3
sum of factors is 1+2+3 = 6 equals original number
 
 
 
Write A Python Function To Check Whether A Number Is Perfect Or Not





Write A Python Function To Check Whether A Number Is Perfect Or Not

Define a function named as check_perfect()  to provide the input argument as number. Take a variable total and set it to zero.


Iterate using the for loop starting from the range of 1 to number - 1,  get the index.   using the If statement try to divide the number by index if the number is completely divisible by index then add the index to the total.


Once the for loop is completed use  another if statement to  check if total is equal to  number if so return  from the function as prime number.

Else return from the function as not a prime number


def check_perfect(number):
   total = 0
   for index in range(1, number):
       if(number % index == 0):
           total += index
   if total == number:
       return "Number is Perfect"
   return "Number is Not Perfect"




In the main function  take a variable and and at a user to enter the number and convert it into an integer. Using the print() function call the check_perfect() function directly.

n = int(input("Enter Number :"))
print(check_perfect(n))




Complete program
==================

def check_perfect(number):
   total = 0
   for index in range(1, number):
       if(number % index == 0):
           total += index
   if total == number:
       return "Number is Perfect"
   return "Number is Not Perfect"


n = int(input("Enter Number :"))
print(check_perfect(n))





Output
==============

Enter Number :6
Number is Perfect




Output
==============

Enter Number :23
Number is Not Perfect




Python Program to Find Perfect Number From 1 to 100

Let's take a range from one to hundred,  and using the for loop iterate from one to hundred and try to call the function to check if the number is prime or not using a function


Complete program
==================
def check_perfect(number):
   total = 0
   for index in range(1, number):
       if(number % index == 0):
           total += index
   if total == number:
       return True
   return False


for x in range(1, 100):
   if check_perfect(x):
       print(f"Number is Perfect {x}")





Output
=================
Number is Perfect 6
Number is Perfect 28





Python Program to Check Perfect Number Using While Loop

Use the while loop to iterate from 1 to the number, get the index,  divide the number by index and keep adding the factors to the total.

Problem with the  while loop  is you have to initialize and increment the index, So increment the index.   Once the while loop completes the execution check if total is equals to the number if return true else false.

In the main program are used to enter a number and call the function,  if the returned value of the function is true to then print the the number is perfect else not perfect number

Complete Program 
================================
def check_perfect(number):
  total = 0
  index = 1
  while index < number:
      if(number % index == 0):
          total += index
      index += 1
  if total == number:
      return True
  return False

n = int(input("Enter Number :"))
if check_perfect(n):
   print("Number is Perfect Number")
else:
   print("Number is Not a Perfect Number")





Output
================

Enter Number :34
Number is Not a Perfect Number




Conclusion
================

Try to run the above programs  by giving the random  integer values as a input  to the program,  note down the result

Comment down if you have any queries or suggestions to improve this  program.





Post a Comment

0 Comments