Here we will write a program in Python to check whether the number is a Perfect number. 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
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.
Now lets write the code for the same.
Python Program to Find Perfect Number
#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!")
Enter any number: 6 The number is a Perfect number!
Please comment for any suggestion or concerns.