Programs · Program 7-7
Q.Program 7-7: Write a program using a user defined function calcFact() to calculate and display the factorial of a number num passed as an argument.
Odisha ChseTextbookSubjective· 3mImportance★★★★★est
23% · 9/40 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Program 7-7 computes a factorial with the multiplicative accumulator pattern: start fact at 1 and multiply it by every integer from num down to 1; calcFact(5) displays 120.
The idea. Factorial (num! = num × (num−1) × … × 1) is the multiplication twin of the summation programs: the accumulator starts at the multiplicative identity 1 (not 0 — multiplying by 0 would destroy everything), and a range with step −1 counts down.
# Program 7-7: factorial of a number passed as an argument
def calcFact(num):
fact = 1
for i in range(num, 0, -1): # num, num-1, ..., 1 (0 excluded)
fact = fact * i
print("Factorial of", num, "is", fact)
calcFact(5)
Dry run for num = 5:
| i | fact |
|---|---|
| start | 1 |
| 5 | 5 |
| 4 | 20 |
| 3 | 60 |
| 2 | 120 |
| 1 | 120 |
Expected output
Factorial of 5 is 120
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.