Home » All Blogs » Python » Python assignments » M.Sc Computer Application » M.Sc Python Programming Assignment 23
M.Sc Python Programming Assignments
Assignment 23
Question:
Write a Python function to check whether a number is perfect or not
Code:
def is_perfect_number(number):
divisors = []
for i in range(1, number):
if number % i == 0:
divisors.append(i)
if sum(divisors) == number:
return True
else:
return False
number = int(input("Enter a positive integer: "))
if is_perfect_number(number):
print(number, "is a perfect number.")
else:
print(number, "is not a perfect number.")
Output:
Enter a positive integer: 33
33 is not a perfect number.