Print all Perfect numbers between 1 to n

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
  1. Take a number as input through the user.
  2. Create an empty list.
  3. Use for loop to iterate over all numbers from 1 to input user.
  4. Understand what is Perfect number from the given example and add such numbers to the list.
  5. 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
				
			

check whether a number is a Perfect number or not

print the Fibonacci series up to n

Leave a Comment