;

Python Program to Find the Strong Number using Factorial Function


Tutorialsrack 25/04/2020 Python

In this Python Program, we will learn how to find the strong number using the factorial() function.

What is a Strong Number?

A Strong Number is a number in which the sum of the factorial of individual digits of that number is equal to the original number. For examples : 1, 2, 145, 40585, etc.

Now take a look at that example, we have a number 145 and its digits are 1, 4, and 5.

Now we find the factorial of each digit as we do

1! = 1 = 1

4! = 4*3*2*1 = 24

5! = 5*4*3*2*1 = 120

Now their sum = 120 + 24 + 1 = 145 which is equal to the Original Number.

Hence We can Say that 145 is a Strong Number.

In this program, we used math.factorial() function which imports from the math module.

Here is the source code of the program to find a strong number using the factorial() function.

Python Program to Find the Strong Number using Factorial Function
# Python Program to find Strong Number using factorial function
 
# Import Module
import math
# Take input From the User
Number = int(input("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("\nSum 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

Enter any Number: 145

Factorial of 5 = 120

Factorial of 4 = 24

Factorial of 1 = 1

 

Sum of Factorials of a Given Number 145 = 145

145 is a Strong Number

 

Enter any Number: 524

Factorial of 4 = 24

Factorial of 2 = 2

Factorial of 5 = 120

 

Sum of Factorials of a Given Number 524 = 146

524 is not a Strong Number


Related Posts



Comments

Recent Posts
Tags