×

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

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Python program to find the area of the triangle

Python program to find the area of the triangle This article will discuss how to find the area of a triangle in Python with all three given sides. The area of...

2 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Gethostbyname() function in 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.

Bar Plot in Python

A bar chart or bar graph displays categorical data using rectangular bars with heights or lengths proportionate to the values for the data distributed in the dataset. In a bar...

16 minutes read.

Linear Regression using Sklearn with Example

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Creating Tables using Python MySQL

In this article, we are going to learn how to create tables in databases using Python MySQL. Introduction to Tables: Generally, databases are used in order to store the information in the...

9 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 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.

Screen Rotation app Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.