# Learn Python – Strong Number in Python- Basic and advance

In this tutorial, we will research a Python program to locate a given number is a Strong wide variety or not.

What is a strong number?

A Strong number is a distinct number whose sum of the all digit factorial must be equal to the wide variety itself.

To discover a whether given quantity is strong or not. We choose every digit from the given wide variety and discover its factorial, and we will do this each and every digit of the number.

Once we get the factorial of all digit, then we do the sum of factorials. If the sum is equal to the given quantity then the given range is robust in any other case not.

For example – The given quantity is 145, we have to select each digit and locate the factorial 1! = 1, 4! = 24, and 5! = one hundred twenty

Now, we will do the sum of the factorials, we get 1+24+120 = 145, which is exactly the same as the given number. So we can say that 145 is a sturdy number.

We obtained the common sense of the sturdy number. Now implements it the usage of the Python Program.

Problem Approach

Ask the consumer to enter an integer number. Find the factorial of every digit in the range using the two while loop. Now, sum up all the factorial number. Check if it is equal to the given number. Print the Output. Exit

Sample Input: num = 132 Sample Output: Given range is not a strong number Explanation: 1! + 3! + 2! = 9 which is not equal to the 132 Sample Input: num = 145 Sample Output: Given wide variety is a strong number.

Python Program to Find Strong Number

Below is the code of the Python software to print the given variety is a sturdy or not.

Example –

``````# Variable to store sum of the numbers
sum=0
# Ask user to enter the number
num=int(input("Enter a number:"))
# temporary variable  store copy of the original number
temp=num
# Using while loop
while(num):
# intialize with 1
i=1
# fact variable with 1
fact=1
rem=num%10
while(i<=rem):
fact=fact*i   # Find factorial of each number
i=i+1
sum=sum+fact
num=num//10
if(sum==temp):
print("Given number is a strong number")
else:
print("Given number is not a strong number")
``````

Output:

``````Enter a number: 145
Given number is a strong number.``````

Explanation:

In the above code

We declared a variable integer value num for enter a number.

Defined the sum variable with zero.

A copy of the num value to the temp variable.

In the first while loop, ensure that the given number is greater than 0.

Inside the while loop, split the number and assigns the variable to find the factorial of each number.

In the second while loop (nested while loop), finds the factorial of the each number.

Suppose user enter the value = 145 and sum = 0

Assigning initial values

``````i = 0
fact = 0
temp = num
temp = 145
``````

Now apprehend the loop iteration. First Iteration

``````rem = temp % 10
rem = 145 % 10 = 5
``````

Now, we entered in the nested while loop. It calculates the factorial of 5 is 120

``````sum = sum + 120> 0+120
sum = 120
temp = temp//10 = 14
temp = 14
``````

Second Iteration

``````temp = 14,
sum = 120
rem = 14 % 10 = 4
``````

Now, it enters into nested While loop. Here, it calculates the factorial of 4 = 24.

``````sum = 120 + 24
sum = 144

temp = 14//10
temp = 1
``````

Third Iteration

``````temp = 1
sum = 144
rem = 1 % 10 = 0
``````

The factorial of 1 is 1

``````sum = 144 + 1
sum = 145
temp = 1 / 10
temp = 0
``````

Here, the temp = zero so, the whilst loop situation fails.

If (num == sum) Now, we test the condition whether or not the consumer enter variety is exactly equal to sum or not. If this situation returns True, then it is strong Number, else it is now not Strong Number.

We have complete the application using the while loop. We can also use for loop to locate whether or not a given wide variety is robust or not.

## Strong Number Using the for loop

We can additionally find the robust quantity using for loop. The good judgment is the identical as above program, the while loop is changed via for loop.

Example –

``````# Python Program to find Strong Number
num = int(input(" Enter the Number:"))
sum = 0
temp = num

while(temp > 0):
fact = 1
rem = temp % 10

for i in range(1, rem + 1):
fact = fact * i

print("Factorial of %d = %d" %(rem, fact))
sum = sum + fact
temp = temp // 10

print("\n Sum of Factorials of a Given Number %d = %d" %(num, sum))

if (sum == num):
print(" The given number is a Strong Number")
else:
print(" The given number is not a Strong Number")
``````

Output:

``````Enter the Number:145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
Sum of Factorials of a Given Number 145 = 145
The given number is a Strong Number``````

## Python Program to find robust quantity the use of factorial characteristic

Python math module offers the built-in math module. By the usage of this method, we can pass over the use of the nested while loop.

Example –

``````# Python Program to find Strong Number

import math
num = int(input(" Enter the Number:"))
sum = 0
temp = num

while(temp > 0):
rem = temp % 10
fact = math.factorial(rem)  # Using the buitlt-in factorial() function

print("Factorial of %d = %d" %(rem, fact))
sum = sum + fact
temp = temp // 10

print("\n Sum of Factorials of a Given Number %d = %d" %(num, sum))

if (sum == num):
print(" The given number is a Strong Number")
else:
print(" The given number is not a Strong Number")
``````

Output:

``````Enter the Number: 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1

Sum of Factorials of a Given Number 145 = 145
The given number is a Strong Number``````

Explanation –

In the above code,

We used the factorial() function and passed a reminder as an argument.

In the first iteration of while loop, it returned the reminder 5 and passed to the factorial of 5.

It will go on until the temp value is greater than zero. We don’t need to use another while loop.