Perfect number Program In Python

      

         Check whether a given number is perfect number or not

 In Python

Program to check whether a given number is a perfect number or not is discussed here. A perfect number is a number which is equal to the sum of its proper positive divisors.

For example, 6 is a perfect number.
The divisors of 6 are 1, 2 and 3.
1 + 2 + 3 = 6.
Program:

    n = int(input("Enter a number : "))
    sum = 0
    for i in range (1,n):
        if(n % i == 0):
            sum = sum + i
    if(sum == n):
        print(n,"is a perfect number")
    else:
        print(n,"is not a perfect number")


    OutPut:
    Enter a number : 6
    6 is a perfect number


    Enter a number : 123
    123 is not a perfect number











    No comments:

    Post a Comment