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

## Python Program to find Strong Number using While Loop

This python program for strong number allows the user to enter any positive integer. Next, it will check 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 Temp variable. It help us to preserve our original value.

First While loop will make sure that, the given number is greater than 0. Statements inside the while loop will split the numbers, and find the factorial of individual digits inside the given number. Please refer Count Number Of Digits in a Number article to understand the logic.

Second While Loop (Nested one) will find the factorial of each digit. I suggest you to refer 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 will enter into Inner or Nested While loop. Here it will calculate 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 will enter into Inner While loop. Here it will calculate 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 will 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 same as above. Here, we replaced 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 same as first example. However, we are using built-in math function called factorial to find the factorial. This will eliminate 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**