Program to Find Perfect Number in Python

Here we will write a program to check whether the number is a Perfect number in Python language. So, first lets start with what is a perfect number.

What is a Perfect number:

Perfect Number is a number in which sum of all its positive divisors excluding that number is equals to the number.
For example:     6
Sum of divisors = 1+2+3 = 6

Step-wise Solution:

1. Take in an integer and store it in a variable.
2. Initialize a variable to count the sum of the proper divisors to 0.
3. Use a for loop and an if statement to add the proper divisors of the integer to the sum variable.
4. Check if the sum of the proper divisors of the number is equal to the variable.
5. Print the final result.
6. Exit.

Now lets write the code for the same.

Here is the program to find perfect number in C++.

Program to Find Perfect Number in Python

#Python Program to Find Perfect Number

n = int(input("Enter any number: "))
sum1 = 0
for i in range(1, n):
    if(n % i == 0):
        sum1 = sum1 + i
if (sum1 == n):
    print("The number is a Perfect number!")
else:
    print("The number is not a Perfect number!")

OUTPUT:perfect number in python

 

Enter any number: 6
The number is a Perfect number!

Please comment for any suggestion or concerns.

Leave a Comment