Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 8 of 13
strong-number-check-with-iterative-factorials.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Calculate factorial function
5def fact(n):
6 if n == 0 or n == 1:
7 return 1
8 result = 1
9 for i in range(2, n + 1):
10 result *= i
11 return result
12
13# Check strong number
14temp = num
15strong_sum = 0
16while temp > 0:
17 digit = temp % 10
18 strong_sum += fact(digit)
19 temp //= 10
20
21if 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. 1

    Define fact using its multiplication loop.

  2. 2

    Extract digits and add fact(digit).

  3. 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!.