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 python 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.

# Python Program to find Strong Number 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 Number %d = %d" %(Number, Sum)) if (Sum == Number): print(" %d is a Strong Number" %Number) else: print(" %d is not a Strong Number" %Number)

**OUTPUT**

**ANALYSIS**

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 in a Number 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 of a Number article to understand the logic behind the factorial.

User Entered value for this Python Program to find Strong Number : Number = 145 and Sum = 0

Factorail = 1, i = 1

Temp = Number

Temp = 145

**Python Program to find Strong Number First While Loop – First Iteration**

Reminder = Temp % 10

Reminder = 145 % 10 = 5

Now, it enters into the 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, it 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 Strong Number.

## Python Program to find Strong Number using For Loop

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

# Python Program to find Strong Number 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 a Strong Number" %Number)

**OUTPUT**

## 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.

# Python Program to find Strong Number 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 a Strong Number" %Number)

**OUTPUT**