Checking perfect number: What is the perfect number? How to check whether a given number is a perfect number or not in Python?
Given a whole number and we need to check whether it is a perfect number or not?
This Python program is utilized to locate its everything positive divisors barring that number.
Clarification: For Example, 28 is a perfect number since divisors of 28 are 1, 2, 4,7,14 at that point total of its divisor is 1 + 2 + 4 + 7 + 14 = 28.
Note: A perfect number is a positive whole number that is equivalent to the aggregate of its legitimate positive divisors.
Python code to discover perfect number:
if __name__ == "__main__" :
# initialisation
i = 2;sum = 1;
# take input from user and typecast into integer
n = int(input("Enter a number: "))
# iterating till n//2 value
while(i <= n//2 ) :
# if proper divisor then add it.
if (n % i == 0) :
sum += i
i += 1
# check sum equal to n or not
if sum == n :
print(n,"is a perfect number")
else :
print(n,"is not a perfect number")
Output:
First run:
Enter a number: 28
28 is a perfect number
Second run:
Enter a number: 14
14 is not a perfect number