Print Perfect Numbers from 1 to 100 in Python

We will develop a program to print perfect numbers from 1 to 100 in Python. Perfect number, a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128.

Program to Print Perfect Numbers from 1 to 100 in Python

This is the simplest and easiest way to print perfect numbers from 1 to 100 in python. We can also take the help of a function. A function is a block of code that performs a specific task. We will take two ranges while declaring the variables. Then, call the function and print all perfect numbers between 1 to 100.

Program description:- Write a program to print all perfect numbers between 1 to 100 in python

# Python program to print perfect numbers from 1 to 100

def perfect_Number(n):  #user-defined function
   if n < 1:
      return False

   perfect_sum = 0
   for i in range(1,n):
      if n%i==0:
         perfect_sum += i
   return perfect_sum == n

# take inputs
min_value = 1
max_value = 100

# calling function and print perfect numbers
print('Perfect numbers from %d to %d are:' %(min_value, max_value))
for i in range(min_value, max_value+1):
   if perfect_Number(i):
      print(i, end=', ')

Output:-

Perfect numbers from 1 to 100 are:
6, 28,

Perfect Numbers Between 1 to 1000 in Python

In this program, we will take ranges from 1 to 1000 while declaring the variables. Then, print perfect numbers between 1 to 1000.

# Python program to print perfect numbers from 1 to 1000

def perfect_Number(n):  #user-defined function
   if n < 1:
      return False

   perfect_sum = 0
   for i in range(1,n):
      if n%i==0:
         perfect_sum += i
   return perfect_sum == n

# take inputs
min_value = 1
max_value = 1000

# calling function and print perfect numbers
print('Perfect numbers from %d to %d are:' %(min_value, max_value))
for i in range(min_value, max_value+1):
   if perfect_Number(i):
      print(i, end=', ')

Output:-

Perfect numbers from 1 to 1000 are:
6, 28, 496,

Python Program to Print Perfect Numbers in Given Range

In the previous program, ranges are hardcoded in the program but in this program, ranges will be provided by the user.

Program description:- Write a python program to print all perfect numbers between given range using functions

# Python program to print perfect numbers in an interval

def perfect_Number(n):  #user-defined function
   if n < 1:
      return False

   perfect_sum = 0
   for i in range(1,n):
      if n%i==0:
         perfect_sum += i
   return perfect_sum == n

# take inputs
min_value = int(input('Print minimum value: '))
max_value = int(input('Print maximum value: '))

# calling function and print perfect numbers
print('Perfect numbers from %d to %d are:' %(min_value, max_value))
for i in range(min_value, max_value+1):
   if perfect_Number(i):
      print(i, end=', ')

Output:-

Print minimum value: 100
Print maximum value: 500
Perfect numbers from 100 to 500 are:
496,

Also See:- Perfect Square in Python

Leave a Comment

Your email address will not be published. Required fields are marked *