Skip to main content

Welcome to Coding 4 Programming

Addition of Two Binary Numbers | Function | Python

To Check Perfect Number | Function | For Loop | Python

Python Program To check whether Entered Number is Perfect Number or Not through user defined Function with For Loop.


According to Wikipedia : In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).

Example : The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128.


Coding of Above Program:

  1. def divisor(n):
  2.     l=[n]
  3.     for i in range(1,n):
  4.         if(n%i==0):
  5.             l.append(i)
  6.     return l
  7. print('''To Check whether the Entered Number
  8. is Perfect Number or Not.''')
  9. n=int(input("Enter Number="))
  10. div=divisor(n)
  11. sum=0
  12. for i in range(0,len(div)):
  13.     sum+=div[i]
  14. if(sum/2==n):
  15.     print("Entered Number is Perfect Number")
  16. else:
  17.     print("Entered Number is Not Perfect Number")

  • Screenshot of Above Coding in Visual Studio Code with Output.
Perfect Number
Perfect Number in Python

Note:
  • If you have any problem related to this program. Please do comment your Problem or Mail us.
  • For illustrating the any concept you can comment by line number.
  • Thank you! For more programs do regular visit to Coding 4 Programming.

Comments

Popular Posts