Strong Number Check

Check if number is strong (sum of factorial of digits equals number).

Logic BuildingIntermediate
Logic Building
# Helper function for factorial
def factorial(n):
    if n == 0 or n == 1:
        return 1
    return n * factorial(n - 1)

# Take number
num = int(input("Enter a number: "))

# Calculate sum of factorial of digits
temp = abs(num)
strong_sum = 0
while temp > 0:
    digit = temp % 10
    strong_sum += factorial(digit)
    temp //= 10

# Check
if abs(num) == strong_sum:
    print("Strong number")
else:
    print("Not a strong number")

Output

Enter a number: 145
Strong number

Sum of factorial of digits equals the number.

Key Concepts:

  • Calculate factorial of each digit
  • Sum all factorials
  • Compare with original number