Strong Number Check with Iterative Factorials
Check strong numbers with iterative digit factorials in Python. Students trace nested multiplication, zero factorial, and the positive-input requirement.
1# Take number2num = int(input("Enter a number: "))34# Calculate factorial function5def fact(n):6 if n == 0 or n == 1:7 return 18 result = 19 for i in range(2, n + 1):10 result *= i11 return result1213# Check strong number14temp = num15strong_sum = 016while temp > 0:17 digit = temp % 1018 strong_sum += fact(digit)19 temp //= 102021if num > 0 and strong_sum == num:22 print("Strong number")23else:24 print("Not a strong number")
Output
Enter a number: 145 Strong number Enter a number: 123 Not a strong number
In this one
- Calculate digit factorials iteratively
- Handle zero factorial
- Compare a positive strong number
How it works
- 1
Define fact using its multiplication loop.
- 2
Extract digits and add fact(digit).
- 3
Require num positive and equal to strong_sum.
Edge cases
Is 0! zero?
Is 0! zero?
No. Zero factorial is one.
Does this helper recurse?
Does this helper recurse?
No. It multiplies factors in a for loop.
What's going on
The helper fact calculates each digit factorial with a loop. Returning one for zero and one gives the correct starting cases without recursive calls.
The outer digit scan sums those helper results. For 145 it gets 1 + 24 + 120 = 145. This version uses the entered positive number directly, unlike the magnitude-based recursive-helper exercise. Zero needs rejection even though an empty scan totals zero.
Is 0! zero? No. Zero factorial is one.
Does this helper recurse? No. It multiplies factors in a for loop.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which digits make 145 strong?
Pick an answer to check it.
Question 2
Is 0! zero?
Pick an answer to check it.
Questions people ask
Is 0! zero?
No. Zero factorial is one.
Does this helper recurse?
No. It multiplies factors in a for loop.
Which digits make 145 strong?
1!, 4!, and 5!.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum