Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Python Program to find Perfect Number

How to write a Python program to find Perfect Number using For Loop, While Loop, and Functions.

Python Perfect Number

Any number can be perfect number in Python, if the sum of its positive divisors excluding the number itself is equal to that number.

For example, 6 is a perfect number in Python because 6 is divisible by 1, 2, 3 and 6. So, the sum of these values are: 1+2+3 = 6 (Remember, we have to exclude the number itself. That’s why we haven’t added 6 here). Some of the perfect numbers are 6, 28, 496, 8128 and 33550336 so on.

Python Program to find Perfect Number using For loop

This Python program for Perfect Number allows the user to enter any number. Using this number it will calculate whether the number is Perfect number or not using the Python For Loop.

# Python Program to find Perfect Number using For loop

Number = int(input(" Please Enter any Number: "))
Sum = 0
for i in range(1, Number):
    if(Number % i == 0):
        Sum = Sum + i
if (Sum == Number):
    print(" %d is a Perfect Number" %Number)
else:
    print(" %d is not a Perfect Number" %Number)

Within this Python perfect number program, the following statement will ask the user to enter any number and stores the user entered value in variable Number

Number = int(input(" Please Enter any Number: "))

Next line, We declared integer variable Sum = 0. Next, we used the for loop with range() and we are not going to explain the for loop here. If you don’t understand for loop then please visit our article Python For Loop.

for i in range(1, Number):

Inside the For loop we placed Python If Statement to test the condition

if(Number % i == 0):
        Sum = Sum + i

If statement inside the for loop will check whether the Number is Perfectly divisible by i value or not. If the Number is perfectly divisible by i then i value will be added to the Sum. Please refer Python If Statement to article to understand the If statement in Python.

Python Program to find Perfect Number using For loop

From the above Python perfect number example, User Entered value is: Number = 6

First Iteration: For the first Iteration, Number = 6, Sum = 0 and i = 1

If (Number % i == 0)
6 % 1 == 0

If statement is succeeded here So,

Sum = Sum + i
Sum = 0 +1 = 1

Second Iteration: For the second Iteration the values of both Sum and i has been changed as: Sum = 1 and i = 2

If (Number % i == 0)
6 % 2 == 0

If statement is succeeded here So,

Sum = Sum + i
Sum = 1 + 2 = 3

Third Iteration: For the third Iteration o this python perfect number program, the values of both Sum and i has been changed as: Sum = 3 and i = 3

If (Number % i == 0)
6 % 3 == 0

If statement is succeeded here So,

Sum = Sum + i
Sum = 3 + 3 = 6

For the fourth and fifth iterations condition inside the if condition will fail

6 % 4 == 0 (FALSE)

6 % 5 == 0 (FALSE)

So, compiler will terminate the for loop.

In the next line we have If statement to check whether the value inside the Sum variable is exactly equal to given Number or Not.

If the condition (Sum == Number) is TRUE then below print statement will execute

print(" %d is a Perfect Number" %Number)

If the condition (Sum == Number) is FALSE then below print statement will be executed

print(" %d is not a Perfect Number" %Number)

For this example (Sum == Number) is True. So, given Number is Perfect Number

Python Program to find Perfect Number using While Loop

This Python perfect number program allows the user to enter any number. Using this number it will calculate whether the user input is Perfect number or not using Python While loop.

# Python Program to find Perfect Number using While loop

Number = int(input(" Please Enter any Number: "))
i = 1
Sum = 0
while(i < Number):
    if(Number % i == 0):
        Sum = Sum + i
    i = i + 1
if (Sum == Number):
    print(" %d is a Perfect Number" %Number)
else:
    print(" %d is not the Perfect Number" %Number)

We haven’t done anything special in this example. We just replaced the For loop in the above python program with While Loop and if you find difficult to understand the While loop functionality then please refer Python While Loop article.

Python Program to find Perfect Number using While loop

Python Program to find Perfect Number using Function

This program for Perfect number allows the user to enter any number. Using this number it will calculate whether the number is Perfect number or not using the Functions.

# Python Program to find Perfect Number using Functions

def Perfect_Number(Number):
    Sum = 0
    for i in range(1, Number):
        if(Number % i == 0):
            Sum = Sum + i
    return Sum        

# Taking input from the user
Number = int(input("Please Enter any number: "))
if (Number == Perfect_Number(Number)):
    print("\n %d is a Perfect Number" %Number)
else:
    print("\n %d is not a Perfect Number" %Number)

If you observe the above code, We haven’t done anything special in this example. We just defined the new user defined function and added the code we mentioned in For loop example.

Python Program to find Perfect Number Using Functions

Python Program to Find Perfect Number between 1 to 1000

This Perfect number program allows the user to enter minimum and maximum values. Next, this Python program will find the Perfect Number between the Minimum and Maximum values.

# Python Program to find Perfect Number between 1 to 1000

# Taking input from the user
Minimum = int(input("Please Enter any Minimum Value: "))
Maximum = int(input("Please Enter any Maximum Value: "))

# initialise sum


# Checking the Perfect Number
for Number in range(Minimum, Maximum - 1):
    Sum = 0
    for n in range(1, Number - 1):
        if(Number % n == 0):
            Sum = Sum + n       
    # display the result
    if(Sum == Number):
        print(" %d " %Number)
Python Program to find Perfect Number between 1 and 1000

Within this perfect number program, this For Loop helps compiler to iterate between Minimum and Maximum Variables, iteration starts at the Minimum and then it will not exceed Maximum variable.

for Number in range(Minimum, Maximum):

Inside the for loop we are checking whether that number is perfect number or not. We already explained the for loop iteration in the first example.

Filed Under: Python Examples

  • Python Hello World Program
  • Python add 2 numbers Program
  • Python Arithmetic Operations
  • Python Calendar Example
  • Python Cube of a Number
  • Python Calculate Electricity Bill
  • Python Calculate Simple Interest
  • Python Compound Interest
  • Python Largest of Two Numbers
  • Python Largest of 3 numbers
  • Python Print Natural Numbers
  • Python natural numbers reverse
  • Python Leap Year Program
  • Python Odd or Even Program
  • Python Even Numbers 1 to N
  • Python Odd Numbers 1 to N
  • Python Positive or Negative num
  • Python Profit or Loss Program
  • Python Square of a Number
  • Python Square root of a Number
  • Python Number Divisible by 5, 11
  • Python Find Power of a Number
  • Python Print Multiplication Table
  • Python Quadratic Equation roots
  • Python Student Grade Program
  • Python Sum of G.P Series
  • Python Sum of A.P Series
  • Python Sum of Series 1³+2³+.+n³
  • Python Sum of Series 1²+2²+.+n²
  • Python Natural num Sum & Avg
  • Python Sum of N natural nums
  • Python Sum of Odd Numbers
  • Python Sum of Even Numbers
  • Python Sum of Even & Odd
  • Python Armstrong number
  • Python Count Digits in a Number
  • Python Fibonacci Series program
  • Python Factorial of a Number
  • Python Factors of a Number
  • Python First Digit of a Number
  • Python GCD of Two Numbers
  • Python Strong Number Program
  • Python Prime Number Program
  • Python Prime Numbers 1 to 100
  • Python LCM of Two Numbers
  • Python natural number in reverse
  • Python Palindrome Program
  • Python Palindrome nums 1-100
  • Python find Perfect Number
  • Python Prime Factors of Number
  • Python Reverse number program
  • Python Strong Number Program
  • Python Strong Numbers 1 to 100
  • Python Sum of Digits of Number
  • Python Swap Two Numbers
  • Python Alphabet or not Program
  • Python Alphabet or Digit
  • Python Digit or not program
  • Python Lowercase or not
  • Python Uppercase or not
  • Python Lowercase or Uppercase
  • Python Vowel or Consonant
  • Python Alphabet digit or special
  • Python ASCII Value of Character
  • Python ASCII String Chars
  • Python Concatenate Strings
  • Python Convert String to Upper
  • Python Convert String to Lower
  • Python Copy a String Program
  • Python Count Vowels in a String
  • Python Count total string chars
  • Python Count Char Occ in String
  • Python Count Total String words
  • Python Last Char Occur in String
  • Python First Char Occur in String
  • Python String Find All Char Occur
  • Python Palindrome String
  • Python Print String Characters
  • Python Replace String character
  • Python remove Odd string Chars
  • Python Reverse a String Program
  • Python String Length Program
  • Python Toggle String Char Case
  • Python List Arithmetic Operation
  • Python Program to Add two Lists
  • Python Count List +Ve & -Ve num
  • Python Even & Odd List nums
  • Python 2nd Largest List Number
  • Python Large & Small List Num
  • Python Largest Number in a List
  • Python List Length
  • Python List Negative Numbers
  • Python List Positive Numbers
  • Python Odd Numbers in a List
  • Python Even Numbers in a List
  • Python Print Elements in a List
  • Python Program to Reverse List
  • Python Sort List in Ascending
  • Python Smallest Number in a List
  • Python Sum of List Even & Odd
  • Python Sum of List Elements
  • Python add key-valuepair to Dict
  • Python Map 2 lists to dictionary
  • Python key exists in Dictionary
  • Python remove dictionary Key
  • Python multiply dictionary items
  • Python Sum of Dictionary Items
  • Python Merge Two Dictionaries
  • Python Print Floyd’s Triangle
  • Python Program for Bubble Sort

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy