Check whether a number is a perfect number or not - Python - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Wednesday 21 October 2020

Check whether a number is a perfect number or not - Python


Hey all, in this amazing tutorial we are going to check whether a number is a perfect number or not in Python. So gear up as we are going to do this program in the best way possible.


As always, you can find the final code of this program at the end of this article. But I would recommend you to be with us this whole journey as we are going to go through each and every step you need to take to become the best coder in the world. Excited? Let's start!


What is a Perfect Number?


As per Wikipedia, a Perfect Number is a number that is equal to the sum of its proper divisors. Understood? Most probably not.


In simple language, if a number is equal to the sum of its divisors excluding itself, then it is a perfect number. 


For example: In the case of the number 6:

Its divisors are 1, 2, 3, and 6. When we exclude 6 and sum up all the other divisors i.e., 1 + 2 + 3, we get 6. Hence, 6 is a perfect number


Some other examples of perfect numbers are 28, 496, and 8128.


I hope that you are now able to understand the basic definition of perfect numbers. Now let's start with the overview.


Overview:


In this question, we are asked to check whether a number is a perfect number or not. The only input required is the number and then we would be using simple for loops and if-else statements to solve the problem.


In short, we would be using for loop to divide the main number with every number from 1 to n-1. And then we would use the if statement to check if the main number is divisible by any number. If it is, we will add the value of the number to a variable called sum. After the loop ends, we will run an if statement to check if the value of sum is equal to the main number. If it does, then the number is a perfect number.


*As I have already told you the basics of the program, it would be good if you try the program on your own. Check if the solution matches!*


Understanding what we need to do:


Here I will tell you step-by-step what you need to do.


Step1: Taking the input from the user


It's the most basic part. We just need to take the input from the user. We will store its value in a variable called num. Don't forget to typecast it into an integer using int.


Here's the code for this step:


Step 2: Checking if the number is a perfect number


Now comes the most important part. After taking the input, I would request you to make another variable called sum and initialize its value to be equal to zero. It would be used later in the program.


To solve this program, we would need to divide the main number with every number below it starting from 1. How do we do it? The simple answer lies in the for loop.


We would make a variable called i which will be iterated in the for loop in range of (1, n) *Remember that range function iterates a value from (starting point, end point-1)*


Then, in the loop, we would use an if statement to check if the main number is divisible by i. And how to check that? We will check that using the % (modulo) function (which tells us the remainder). If the value when num%i is equal to 0, then we would add the value of i to the value of sum.


I hope now you are able to understand what are we trying to do.


This loop will run until it does the same things with all the numbers below the main number. When it ends, we would get the sum of all its divisors (excluding itself). 


Next, we would use an if statement to check if this sum is equal to num. And if it is, then the number is a perfect number. Else, if it isn't then it is not a perfect number. 


Here's the code for this step:


So now, we have successfully found the best way to check whether a number is a perfect number or not!


Here's the final code:



Here are some images of the output of our program:


Check whether a number is a perfect number or not - Python


Check whether a number is a perfect number or not - Python


And here ends our journey where we used various concepts to find the solution to this problem. I hope you liked this tutorial. And if you did, then don't forget to share it. It may help someone struggling with this code.


Also read: WAP (Python) to check if a number is an Armstrong Number or not


Also, please consider subscribing to our newsletter. We do these coding sessions regularly. You will surely love them!


Again, thanks for reading!

No comments:

Post a Comment