Q:

Write python program that asks the user to enter a positive integer N and print if N is a perfect number or not

0

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

For example:

  • 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.
  • 28 is a perfect number because 28=1+2+4+7+14

Write a python program that asks the user to enter a positive integer N and print if N is a perfect number or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#Write a program that asks the user to enter a positive integer N and print if N is a perfect number or not
N=int(input('enter a positive integer:'))
if N<0:
    print('invalid entry, you should enter a positive integer!')
else:
    #here we will check if N is perfect or not
    divisorsSummation=0
    for i in range(1,N):
        if N%i==0: #i is divisor on N
          divisorsSummation=divisorsSummation+i
    if divisorsSummation==N:
        print('number is perfect')
    else: print('number is not perfect')

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now