# Python Program to find Strong Number

Write a Python Program to find Strong Number using While Loop, For Loop, and factorial function with an example.

## Python Program to find Strong Number using While Loop

This program for a strong number allows the user to enter any positive integer. Next, Python checks whether the given number is a Strong Number or Not using the While Loop.

```Number = int(input(" Please Enter any Number: "))
Sum = 0
Temp = Number

while(Temp > 0):
Factorial = 1
i = 1
Reminder = Temp % 10

while(i <= Reminder):
Factorial = Factorial * i
i = i + 1

print("\n Factorial of %d = %d" %(Reminder, Factorial))
Sum = Sum + Factorial
Temp = Temp // 10

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

if (Sum == Number):
print(" %d is a Strong Number" %Number)
else:
print(" %d is not" %Number)
```

Within this Python strong number example, First, we are assigning the original value to the Temp variable. It helps us to preserve our original value.

The first While Loop makes sure that the given number is greater than 0. Statements inside the while loop split the numbers, and find the factorial of individual digits inside the given number. Please refer to Python Count Number Of Digits article to understand the logic.

The second While Loop (Nested one) finds the factorial of each digit. I suggest you refer to Python Find Factorials article to understand the logic behind the factorial.

User Entered value for this Python Program to find Strong Number = 145 and Sum = 0
Factorail = 1, i = 1
Temp = Number
Temp = 145

First While Loop – First Iteration
Reminder = Temp % 10
Reminder = 145 % 10 = 5

Now, it enters into the Python inner or Nested While loop. Here, it calculates the factorial of 5 = 120.

Sum = Sum +120 => 0 + 120
Sum = 120

Temp = Temp //10 => 145 //10
Temp = 14

Second Iteration
Temp = 14 and Sum = 120
Reminder = 14 % 10 = 4

Now, it enters into Inner While loop. Here, Python program to find strong number calculates the factorial of 4 = 24.

Sum = 120 + 24
Sum = 144

Temp = 14 /10
Temp = 1

Third Iteration
Temp = 1 and Sum = 144
Reminder = 1 % 10 = 0

Here, the factorial of 1 is 1
Sum = 144 + 1
Sum = 145

Temp = 1 / 10
Temp = 0

Here Temp = 0 so, the while loop condition fails.

if ( Number == Sum ) – Condition check whether the user enter number is exactly equal to Sum or not. If this condition is True, then it is Strong Number, else it is not.

## Python Program to find Strong Number using For Loop

This program for Python strong number is the same as above. In this program, we replaced the While loop with For Loop.

```Number = int(input(" Please Enter any Number: "))
Sum = 0
Temp = Number

while(Temp > 0):
Factorial = 1
Reminder = Temp % 10

for i in range(1, Reminder + 1):
Factorial = Factorial * i

print("Factorial of %d = %d" %(Reminder, Factorial))
Sum = Sum + Factorial
Temp = Temp // 10

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

if (Sum == Number):
print(" %d is a Strong Number" %Number)
else:
print(" %d is not" %Number)```
`````` Please Enter any Number: 40585
Factorial of 5 = 120
Factorial of 8 = 40320
Factorial of 5 = 120
Factorial of 0 = 1
Factorial of 4 = 24

Sum of Factorials of a Given Number 40585 = 40585
40585 is a Strong Number``````

## Python Program to find Strong Number using factorial function

This Python strong number program is the same as the first example. However, we are using a built-in math function called factorial to find the factorial. This approach eliminates the Nested while loop.

```import math
Number = int(input(" Please Enter any Number: "))
Sum = 0
Temp = Number

while(Temp > 0):
Reminder = Temp % 10
Factorial = math.factorial(Reminder)

print("Factorial of %d = %d" %(Reminder, Factorial))
Sum = Sum + Factorial
Temp = Temp // 10

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

if (Sum == Number):
print(" %d is a Strong Number" %Number)
else:
print(" %d is not" %Number)```
`````` Please Enter any Number: 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1

Sum of Factorials of a Given Number 145 = 145
145 is a Strong Number``````