×

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

Python Empty Tuple

How to Create an Empty Tuple Tuple A tuple is a data structure used to store non-homogeneous data elements. These non-homogeneous data elements consist of integer data type, character data type, String...

3 minutes read.

Python Scikit-image | Image Processing Using Scikit-Image

What is Image Processing? The world is defined with images and, every image has its different specialties. An image can contain much-needed information that can be helpful in various ways. The process...

4 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

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

3 minutes read.

Python String replace() method

Python String replace() method The string.replace() method in Python returns a copy of the string with all occurrences of substring old replaced by new. Syntax replace(old, new[, count]) Parameter old: This parameter represents a substring you want to...

2 minutes read.

Python String islower() method

Python String islower() method The string.islower() method returns a boolean value true if all cased characters in the string are lowercase and for  any other value is returns false otherwise. Syntax string.islower() Parameter NA Return This...

1 minute read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes 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.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Compound Interest GUI Calculator using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

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

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

App Config Python

An XML file called App. Config serves as the document for any programme. In other terms, you can modify any configuration inside of it without going to edit the code...

7 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.