Q:

Python program to find perfect number

belongs to collection: Python basic programs

0

Given an integer number and we have to check whether it is perfect number or not?

This Python program is used to find its all positive divisors excluding that number.

Explanation: For Example, 28 is a perfect number since divisors of 28 are 1, 2, 4,7,14 then sum of its divisor is 1 + 2 + 4 + 7 + 14 = 28.

All Answers

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

Python code to find 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

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

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to print perfect numbers from the g... >>
<< Python program to lowercase the character without ...