Write a Python Program to find a Strong Number using While Loop, For Loop, and factorial functions 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, this Python program 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 Program to find the 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 the 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 the Python Find Factorials article to understand the logic behind the factorial.
The user Entered values for this Python Program to find Strong Number = 145 and Sum = 0
Factorial = 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 the Inner While loop. Here, the Python strong number program 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 entered number is exactly equal to Sum or not. If this condition is True, then it is a Strong Number. Else, it is not.
Python Program to find Strong Number using For Loop
This program for strong numbers 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 %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 40585 = 40585
40585 is a Strong Number
Python Program to find Strong Number using factorial function
This 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 %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 145 = 145
145 is a Strong Number
Using list comprehension
This program code uses list comprehension and the math factorial function to check and find the string number in Python.
import math def isStrongNumber(num): digits = list(map(int, str(num))) tot = sum(math.factorial(d) for d in digits) return tot num = int(input("Enter any Number: ")) if (num == isStrongNumber(num)): print(num, " is a Strong Number.") else: print(num, " is not.")
Enter any Number: 145
145 is a Strong Number.
Program to Check Strong Number in Python using Recursion
def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) def isStrongNumber(num): digits = list(map(int, str(num))) tot = sum([factorial(i)for i in digits]) return tot num = int(input("Enter any Number: ")) if (num == isStrongNumber(num)): print(num, " is a Strong Number.") else: print(num, " is not.")
Enter any Number: 153
153 is not.
Using lambda and reduce functions
In this example, first, we have to import the reduce function from the functools and concat multiple lambda statements.
from functools import reduce def isStrongNumber(num): digits = list(map(int, str(num))) tot = reduce(lambda x, y: x + y, [reduce(lambda x, y:x * y, range(1, d + 1)) for d in digits]) return tot num = int(input("Enter any Number: ")) if (num == isStrongNumber(num)): print(num, " is a Strong Number.") else: print(num, " is not.")
Enter any Number: 225
225 is not.