Strong Number in Python
In this tutorial, we will learn a Python program to find a given number is a Strong number or not.
What is a strong number?
A Strong number is a special number whose sum of the all digit factorial should be equal to the number itself.
To find a whether given number is strong or not. We pick each digit from the given number and find its factorial, and we will do this 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 number then the given number is strong otherwise not.
For example – The given number is 145, we have to pick each digit and find the factorial 1! = 1, 4! = 24, and 5! = 120.
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 strong number.
We got the logic of the strong number. Now implements it using the Python Program.
Problem Approach
- Ask the user to enter an integer number.
- Find the factorial of each digit in the number 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 number is not a strong number
Explanation: 1! + 3! + 2! = 9 which is not equal to the 132
Sample Input: num = 145
Sample Output: Given number is a strong number.
Python Program to Find Strong Number
Below is the code of the Python program to print the given number is a strong or not.
Example –
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
Now understand the loop iteration. First Iteration
Now, we entered in the nested while loop. It calculates the factorial of 5 is 120.
Second Iteration
Now, it enters into nested While loop. Here, it calculates the factorial of 4 = 24.
Third Iteration
The factorial of 1 is 1
Here, the temp = 0 so, the while loop condition fails.
If (num == sum) Now, we check the condition whether the user enter number is exactly equal to sum or not. If this condition returns True, then it is strong Number, else it is not Strong Number.
We have complete the program using the while loop. We can also use for loop to find whether a given number is strong or not.
Strong Number Using the for loop
We can also find the strong number using for loop. The logic is the same as above program, the while loop is replaced by for loop.
Example –
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 strong number using factorial function
Python math module provides the built-in math module. By using this method, we can omit the use of the nested while loop.
Example –
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.