Javatpoint Logo
Javatpoint Logo

Perfect Number in Python

Playing with numbers is something that we are doing since our childhood. The basic arithmetic operations performed on numbers generate valuable results. In the voyage of learning, we must give more emphasis to the logic develop in mathematics as they become the foundation of programming.

In this article, let us discuss what is a perfect number, what are the steps to determine whether a number is perfect or not, and finally, we shall see how its program can be written in Python.

So, let's get started…

First, let us understand what is a perfect number?

A perfect number is a number in which the sum of the divisors of a number is equal to the number.

Make sure that we have to exclude the number when we are calculating the sum of divisors.

Now, we will see what are the steps involved in determining the perfect number.

  1. First of all, we will ask the user to input an integer which will be stored in a variable.
  2. Now, we will declare a variable called 'sum', where we will store the sum of the divisors of the given number.
  3. The next task is, to use a for loop where we will divide our number with the number initialized for the variable i, then we'll increment the value of I and check what are the numbers give us the remainder as zero. These numbers will be our divisors.
  4. Now we will take each of the divisors of that number and add it with the variable 'sum'.
  5. Finally, we will use the decision statement keyword 'if' to compare the number given by user with the value of the sum.
  6. If the values are equal, we will display the result as 'It's a perfect number', else we will display, 'It's not a perfect number'.

It's time to have a look at the Python program-

Example -


Enter the number: 6
The entered number is a perfect number
Enter the number: 25
The entered number is not a perfect number

Explanation -

In the program given above, we have-

  1. We have a variable 'num' and asked the user to provide its value.
  2. As discussed earlier, we have declared the variable sum_v as zero.
  3. Now, we have used the for loop whose range is from 1 to the number provided by the user.
  4. The next step is to check whether the number when divided with all the numbers provided in the range gives the remainder as zero.
  5. Those values will be added and then stored in the variable sum_v.
  6. Finally, we will make use of 'if' to compare the sum of divisors with the number and display the required result.
  7. In this program, the user has given the values 6 and 25 and the desired output is displayed.

Let us have a look at another program where we will implement the same with the help of a function.

Example - 2:



Explanation -

In this program, we have used steps similar to the ones we discussed in the previous example, the only difference is here we have defined a function and called it by providing a value.

Because the return statement is present at the end of a function definition, it gives the value as True and False.

So, in this tutorial, we learned about perfect numbers, the process that we must follow to determine whether the given number is perfect or not, and at the end discussed its implementation in Python.

Next TopicEOL in Python

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA