9.What is the output of the following pseudocode program, which first defines a function
functn and then calls it from a for-loop?
def functn(n)
if n==1
return 1
else
return n*functn(n-1)
endif
enddef
for i = 1 to 5
print functn(i)
endfor
(a) 1 2 3 4 5
(b) 1 1 2 3 5
(c) 1 2 6 24 120
(d) None of the above
Pseudocode Recursive Factorial Output for i=1 to 5
The pseudocode defines a recursive function functn(n) that computes the factorial of n, returning 1 for n==1 and n * functn(n-1) otherwise. The for-loop calls print functn(i) for i=1 to 5, producing outputs 1, 2, 6, 24, 120—matching option (c).
Step-by-Step Execution
Trace the recursion for each call:
-
functn(1): Hits base casen==1, returns 1. -
functn(2): Returns 2×functn(1)=2×1=2. -
functn(3): Returns 3×functn(2)=3×2=6. -
functn(4): Returns 4×functn(3)=4×6=24. -
functn(5): Returns 5×functn(4)=5×24=120.
This matches the standard factorial sequence where n!=n×(n−1)!.
Option Analysis
| Option | Output Sequence | Explanation |
|---|---|---|
| (a) | 1 2 3 4 5 | Incorrect; represents linear values i, not recursive products. Ignores factorial multiplication. |
| (b) | 1 1 2 3 5 | Incorrect; resembles Fibonacci but fails factorial recursion (e.g., ignores 3!=6). |
| (c) | 1 2 6 24 120 | Correct; exact factorial values: 1!=, 2!=, 3!=, 4!=, 5!. |
| (d) | None of the above | Incorrect; (c) matches precisely. |


