# Strong Number Program in Python

Strong Number Program in Python | A strong number is a special number in which where the sum of all digit factorial is equal to the sum itself.  For example, consider 145 = 1! + 4! + 5! = 145, Sum of digit factorial in the sense 1! added to 4! again added to 5! is 145, hence 145 is strong number. Now let us see the Python program for strong numbers. Also See:- Armstrong Number Program in Python

We will see these below Python program examples:–

• Strong Number In Python Using For Loop
• Strong Number In Python Using While Loop
• Python Program To Find Strong Number In a List
• Strong Number In Python Using Function

## Strong Number in Python using For Loop

In this program to find a Strong number in Python, we have used a for loop.

``````number = int(input("Enter number: "))
s = 0
temp = number
while(temp > 0):
fact = 1
rem = temp % 10
for i in range(1, rem + 1):
fact = fact * i
print("Factorial of %d = %d " %(rem, fact))
s = s +fact
temp = temp // 10

print("Sum of factorials of the number %d = %d " %(number,s))
if(s == number):
print("Strong Number")
else:
print("Not a strong number")``````

Output for the input values test-case-1:-

Enter number: 135
Factorial of 5 = 120
Factorial of 3 = 6
Factorial of 1 = 1
Sum of factorials of the number 135 = 127
Not a strong number.

Output for the input values test-case-2:-

Enter number: 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
Sum of factorials of the number 145 = 145
Strong Number.

In this code, we take input from the user, and then initialize s to 0 then equate number to temp Next, we use a while loop, and in for loop, we traverse over the number.

## Strong Number in Python using While Loop

Similar to for loop we use while loop to find strong numbers.

``````s = 0
number = int(input("Enter number: "))
temp = number
while(number):
i = 1
facto = 1
remainder = number % 10
while(i <= remainder):
facto = facto * i
i = i + 1
s = s + facto
number = number // 10

if(s == temp):
print("Strong number")
else:
print("not a strong number")``````

Output for the input values test-case-1:-

Enter number: 145
Strong number

Output for the input values test-case-2:-

Enter number: 135
not a strong number

While a loop is a loop that executes until the condition becomes false, hence this loop is more used when we need to iterate over the same thing again and again. In the code, we have used a while loop twice i the first while loop we check for the numbers of digits in the given number, and in the second loop, we find the factorial of digits.

## Python Program to Find Strong Number in a List

We use a user-defined function to find a strong number, by using a user-defined function enables the reusability of code, that is there is no need to write the code again and again. Once the function is defined we can call it where ever necessary.

``````def factorial(num):
if(num == 0 or num == 1):
fact = 1
else:
fact = num * factorial(num - 1)
return fact

def strong_num(list):
list1 = []
for x in list:
temp = x
sum = 0
while(temp):
remainder = temp % 10
sum += factorial(remainder)
temp = temp // 10
if(sum == x):
list1.append(x)
else:
pass
return list1

list = [9,7,6,4,1,2,145]
strong = strong_num(list)
print(strong)``````

Output:-

[1, 2, 145]

Strong numbers in the above ist are 1,2 and 145.

In this code, we use two user-defined function factorial() and strong_num() which takes a parameter number and list respectively. The factorial() finds the factorial of each number and strong_num() finds strong numbers present in a list. Then in the main code, we will take a list and call strong_num() to find a strong number present in a list. This strong_num() function traverses through the list and finds the strong number present.

## Strong Number Program in Python using Function

Previously, we saw how to find strong numbers using the user-defined function, now in this section, we use the built-in function available in python to find the strong number.

``````import math
number = int(input("Enter Number: "))
sum = 0
temp = number
while(temp>0):
remainder = temp % 10
fact = math.factorial(remainder)
print("Factorial of %d = %d" %(remainder, fact))
sum = sum + fact
temp = temp // 10

print("Sum of factorials of a number %d = %d" %(number,sum))
if(sum == number):
print("Strong number")
else:
print("not a strong number")``````

Output for the input values test-case-1:-

Enter Number: 145
Factorial of 5: 120
Factorial of 4: 24
Factorial of 1: 1
Sum of factorials of a number 145 = 145
Strong number

Output for the input values test-case-2:-

Enter Number: 154
Factorial of 4: 24
Factorial of 5: 120
Factorial of 1= 1
Sum of factorials of number 154 =145
Not a strong number

Here, we use a pre-defined function called factorial() which is available in the math library, hence we have imported math. This factorial() by itself calculated the factorial of a single digit. Therefore, it reduces our effort of calculating the factorial of each digit every time.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!