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 range23import math45start = int(input("Enter start of range: "))6end = int(input("Enter end of range: "))78for num in range(start, end + 1):9 total = 010 temp = num11 while temp > 0:12 digit = temp % 1013 total += math.factorial(digit)14 temp //= 1015 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.