×

Python program for perfect number

Python program for perfect number

Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what a perfect number is.

Perfect number

If a given number is equal to the sum of its all divisors, then we call such number as 'Perfect number.'

Example of perfect number: 6 is a perfect number because sum of all divisors of 6 i.e., 1, 2 & 3, is equal to 6 (1 + 2 + 3 = 6). But, in the same way, 15 is not a perfect number.

A Python Program to Check Perfect Number

In this tutorial, we will write a program so that we can check if the given number in the program is perfect or not. We will take the number as user input and return the output that the number given by the user is a perfect number or not.

We will implement the following steps in the program for checking perfect number:

Step 1: We will take a positive integer from the user and store it in a num variable.

Step 2: We will initialize the num variable to find out the proper sum of divisors up to 0.

Step 3: Then, we will use a for loop in the program and if statement to add all the proper divisors of the number given as input.

Step 4: We will use the if else statement condition to check if the sum of all divisors is equal to the actual number or not.

Step 5: If the condition satisfies, the program will print, "The number given by you is a perfect number!" Otherwise, the program will print, "The number given by you is not a perfect number!"

Step 6: Exit the function.

Let's understand the implementation of above steps through the following Python program:

Example – 

 # Define default perfect number checker function
 def PerChecker (a):
     sums = 0 # sum variable
     for j in range (1, a): # for loop to find total sum
         if (a % j == 0):
             sums = sums + j # sum of all values
     if (sums == a): # a perfect number
        print ("The number" ,a, " is a Perfect number!")
     else: # not a perfect number
         print ("The number" ,a, " is not a Perfect number!")    
 # Taking number from user as input
 num = int (input ("Enter a positive integer: "))
 # Calling out function
 PerChecker (num) 

Output:

 Enter a positive integer: 6
 The number 6 is a Perfect number!
 Enter a positive integer: 37
 The number 37 is not a Perfect number! 

Explanation – 

As we can see that, when we gave six as user input, the program return that it is a perfect number, but when we gave 37 as user input, the program returned 37 is not a perfect number. So, we can check any number is a perfect number or not in our Python program by implementing the above-explained approach.


Related Topics

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Paramiko Python Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

Speech Recognition in Python

What is Speech Recognition? Speech Recognition is a term defined for automatic recognition of human speech. Speech recognition is the most significant activity in the domain of the interaction between the...

8 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

How to run Python code from the command prompt

The Windows operating system's command-line interpreter is CMD or Command Prompt. The "MS-DOS Prompt" is comparable to Command.com, used in DOS and Windows 9x computers. It is similar to Unix...

3 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Python Typing Module

An Introduction to the Typing Module The typing module is introduced in Python version 3.5 and used to provide hinting method types in order to support static type checkers and linters...

10 minutes read.

Python elif

Python elif The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them. Syntax if expression1: statement elif expression2: statement elif expression3: statement else: statement The elif statement can be optional...

3 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Python Program to Print all the Prime Number in an Interval

Python Program to Print all the Prime Number in an Interval What is Prime numbers? A prime number is referred to those numbers that can be divisible by them only. In simple words,...

4 minutes read.

Python Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.