Strong Numbers in Range in Python

Print all strong numbers in a given range.

BeginnerLoop ProgramsExample 19 of 25
strong-numbers-in-range.py
1# Program to print strong numbers in a range
2
3import math
4
5start = int(input("Enter start of range: "))
6end = int(input("Enter end of range: "))
7
8for num in range(start, end + 1):
9 total = 0
10 temp = num
11 while temp > 0:
12 digit = temp % 10
13 total += math.factorial(digit)
14 temp //= 10
15 if total == num:
16 print(num)

Output

Enter start of range: 1
Enter end of range: 500
1
2
145

What's going on

We reuse the strong-number definition for every number in the range and print matches.

After this program

If the idea is still fuzzy, read Day 9: Loops and Iteration in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 loop programs, or see every topic on Python exercises.