In this program, we will print all Perfect numbers between 1 to n.
Definition of perfect number: A positive integer number that is equal to the sum of its proper divisors. The smallest perfect number is 6, its divisors are 1, 2, and 3 and their total sum is 6.
Other perfect numbers are 28, 496, and 128.
Steps to solve the program
- Take a number as input through the user.
- Create an empty list.
- Use for loop to iterate over all numbers from 1 to input user.
- Understand what is Perfect number from the given example and add such numbers to the list.
- Print the output.
num = int(input("Enter a number: "))
for i in range(1,num):
total = 0
for j in range(1,i):
if i % j == 0:
total += j
if total == i:
print(i, end=' ')
Output :
Enter a number: 50
6 28