In: Computer Science
in PYTHON
Determine whether a number provided by the user is perfect or not.
write a program that:
1. Ask the user for a positive integer
2. Compute the list of integer divisors of the given number
3. Check whether the given number is perfect
4. Communicate the result to the user (something like ‘Your number…. Is a perfect number’ or ‘Your number ….. is not a perfect number’.
5. In any case, communicate to the user the list of divisors.
Python code:
#accepting number
num=int(input("Enter a positive integer: "))
#initializing list of divisors
lis=[]
#looping till number
for i in range(1,num):
#checking if current number is a divisor
if(num%i==0):
#adding it to lis
lis.append(i)
#checking if the number is perfect
if(sum(lis)==num):
#printing perfect
print("Your number "+str(num)+" is a perfect
number")
else:
#printing not perfect
print("Your number "+str(num)+" is not a perfect
number")
#printing list of divisors
print("The list of divisors are ",end="")
#printing list
print(lis)
Screenshot:
Input and Output: