Exercise · Q15
Q.Write a pseudocode to calculate the factorial of a number (Hint: Factorial of 5, written as 5! = 5×4×3×2×1).
Lakshadweep CbseNCERTSubjective· 3mImportance★★★★★est
92% · 33/36 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 →Keep a running product fact starting at 1 and multiply it by every counter value from 1 to num — for 5 the product grows 1, 2, 6, 24, 120.
The idea (accumulation by multiplication). Factorial is repeated multiplication: 5! = 5×4×3×2×1. Just as a running sum starts at 0, a running product must start at 1 (the multiplicative identity); a counter-controlled loop then folds each integer into the product.
Pseudocode
FACTORIAL_ALGO
Step 1: INPUT num
Step 2: SET fact = 1
Step 3: SET i = 1
Step 4: WHILE i <= num, REPEAT Steps 5-6
Step 5: fact = fact * i
Step 6: i = i + 1
Step 7: PRINT "Factorial =", fact
END
Python verification
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact = fact * i
print("Factorial =", fact)
Dry run for num = 5
| Pass | i | fact = fact × i |
|---|---|---|
| 1 | 1 | 1 × 1 = 1 |
| 2 | 2 | 1 × 2 = 2 |
| 3 | 3 | 2 × 3 = 6 |
| 4 | 4 | 6 × 4 = 24 |
| 5 | 5 | 24 × 5 = 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.