Python Program to check the number is strong number


February 23, 2022, Learn eTutorial
1075

In this simple python program, we need to check the given number is a strong number. It is a number-based python program.

For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:

What is a strong number?

To solve the basic python program, we need to know what is a strong number. A number is said to be strong if the sum of the Factorial of the individual digits of the number will be equal to the number itself. Let us take an example to understand more clearly.

let's take a number 145 and check if the number is a strong number by adding the factorial of 1 and 4 and 5 which will be, 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145. so the number and the sum are equal, and 145 is a Strong number.

How we check a strong number in python?

To apply this logic in the python programming language, we have to calculate the factorial of individual digits of the number, initialize a sum variable to zero, and accept the number from the user we store the number temp variable. Now we open a python while loop till the number, and we split the number by using the mod operator and take one digit from the number.

Now open another while loop from 1 to the digit. And do a multiplication factorial f=f*i [till we reach the number with the multiplication variable] and increment the 'i' to multiply all numbers till the digit. After the while loop adds the factorial of that digit to the sum, divide the number by 10 removing the last digit. And make the loop again. Finally, check the sum and number are equal using an  if condition in python, if they are equal, print it's a strong number.

ALGORITHM

STEP 1: Accept the number from the user using the input function in python programming and initialize a sum variable

STEP 2: Store the number into a temp variable.

STEP 3: Open a while loop to split the number and take a digit, using python syntax.

STEP 4: Initialize 'i' and f variables to 1.

STEP 5: Split and take one digit from the number using the mod operator.

STEP 6: Open another while loop from 1 to the digit.

STEP 7: Use f = f*i to get the factorial of the digit, increment the 1 with 1 every time in the loop.

STEP 8: Add the factorial of the digit to the sum. for checking the number and sum are equal

STEP 9: Remove that digit from the number by dividing by 10.

STEP 10: Using the if condition checks the sum and number are equal, and print its strong number if the condition satisfied using python programming basics.

Python Source Code

                                          sum1=0
num=int(input("Enter a number:"))
temp=num
while(num):
    i=1
    f=1
    r=num%10
    while(i<=r):
       f=f*i
       i=i+1
    sum1=sum1+f
    num=num//10
if(sum1==temp):
    print("The number is a strong number")
else:
    print("The number is not a strong number")
                                      

OUTPUT

Enter a number:145

The number is a strong number.