Check Strong Number in Python

Check whether a number is a strong number (sum of factorials of digits).

BeginnerConditional ProgramsExample 9 of 20
check-strong-number.py
1# Program to check strong number
2
3import math
4
5num = int(input("Enter an integer: "))
6
7total = 0
8temp = num
9while temp > 0:
10 digit = temp % 10
11 total += math.factorial(digit)
12 temp //= 10
13
14if total == num:
15 print(num, "is a strong number")
16else:
17 print(num, "is not a strong number")

Output

Enter an integer: 145
145 is a strong number

What's going on

A strong number equals the sum of factorials of its digits (e.g., 145 = 1! + 4! + 5!).

We extract digits with modulo and integer division, summing factorials.

After this program

If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.