For example, 145 is a Strong number because:
1! + 4! + 5! = 1 + 24 + 120 = 145
Some other examples of Strong numbers are: 1, 2, 145, 40585, 871, and 1454.
Here's a Python program to check if a given number is a Strong number or not:
python# Function to calculate the factorial of a number
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
# Function to check if a number is a Strong number
def is_strong(n):
# Compute the sum of the factorials of the digits
sum = 0
temp = n
while temp > 0:
digit = temp % 10
sum += factorial(digit)
temp //= 10
# Check if the number is a Strong number
if n == sum:
return True
else:
return False
# Get the number to check from the user
num = int(input("Enter a number: "))
# Check if the number is a Strong number
if is_strong(num):
print(num, "is a Strong number")
else:
print(num, "is not a Strong number")
In this program, I first define a function factorial()
to calculate the factorial of a number. This function takes a number n
as input and returns its factorial.
I then define a function is_strong()
to check if a number is a Strong number or not. To do this, I compute the sum of the factorials of the digits in the number. I extract each digit using the modulus operator (%
), compute its factorial using the factorial()
function, and add it to a running sum. Finally, I check if the sum is equal to the original number, and return True
if it is, or False
otherwise.
I then ask the user to enter a number to check. I call the is_strong()
function on the number, and print a message indicating whether it's a Strong number or not based on the function's return value.