import math
def is_strong_number(number):
sum_of_factorials = sum([math.factorial(int(digit)) for digit in str(number)])
return sum_of_factorials == number
# Input from user
num = int(input("Enter a number: "))
if is_strong_number(num):
print(f"{num} is a strong number.")
else:
print(f"{num} is not a strong number.")
Code Explanation:
Importing the Math Library
import math
import math: Brings the math library into the program, giving access to mathematical functions like math.factorial().
2. Defining the Function
def is_strong_number(number):
def: Declares a new function.
is_strong_number: The name of the function, which determines if a given number is a strong number.
number: The input parameter for the function, representing the number to check.
3. Calculating the Sum of Factorials of Digits
sum_of_factorials = sum([math.factorial(int(digit)) for digit in str(number)])
str(number): Converts the number into a string so that we can loop through its digits.
int(digit): Converts each digit (originally a string) back to an integer.
math.factorial(int(digit)): Calculates the factorial of the digit.
sum([...]): Calculates the sum of the list.
sum_of_factorials: Stores the result of this calculation.
4. Checking if the Number is a Strong Number
return sum_of_factorials == number
sum_of_factorials == number: Compares the sum of the factorials of the digits to the original number.
If they are equal, the number is a strong number.
Returns True if the number is strong; otherwise, returns False.
5. Taking Input from the User
num = int(input("Enter a number: "))
input("Enter a number: "): Displays a prompt and takes input from the user as a string.
int(): Converts the string input to an integer.
num: Stores the user-provided number.
6. Checking and Printing the Result
if is_strong_number(num):
print(f"{num} is a strong number.")
else:
print(f"{num} is not a strong number.")
if is_strong_number(num):: Calls the is_strong_number function with the user’s input (num).
If the function returns True, it means num is a strong number.
If it returns False, num is not a strong number.
print(f"..."): Displays the result to the user using an f-string.
#source code --> clcoding.com