This is a Python Program to check if a number is a Perfect number.
The program takes a number and checks if it is a Perfect number.
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.
Here is source code of the Python Program to check if a number is a Perfect number. The program output is also shown below.
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!")
1. User must enter the number and store it in a variable.
2. Use a for loop to generate numbers from 1 to n (where n is not included as we need the sum of the proper divisors of the number).
3. Using an if statement check if the number divided by i gives the remainder as 0 which is basically the proper divisor of the integer.
4. Then the proper divisors of the number are added to the sum variable.
5. If the sum of the proper divisors of the number is equal to the original number, tje number is a Perfect number.
6. The final result is printed.
Case 1: Enter any number: 6 The number is a Perfect number! Case 2: Enter any number: 25 The number is not a Perfect number!
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Check Python Books
- Apply for Programming Internship
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Python Internship