ITPEC FE Subject B April 2025 Question 3
ITPEC FE Subject B April 2025 — Question 3 of 20
GCD by Subtraction (Euclidean Algorithm) — trace variable values through a while loop.
The function GCD(98, 56) uses repeated subtraction to find the greatest common divisor. The output m, n statement is inside the loop, after the if/else block, so it prints values after each subtraction.
Trace:
| Iteration | m (before) | n (before) | Condition | Action | Output |
|---|---|---|---|---|---|
| 1 | 98 | 56 | m > n | m ← 98 − 56 = 42 | 42 56 |
| 2 | 42 | 56 | m < n | n ← 56 − 42 = 14 | 42 14 |
| 3 | 42 | 14 | m > n | m ← 42 − 14 = 28 | 28 14 |
| 4 | 28 | 14 | m > n | m ← 28 − 14 = 14 | 14 14 |
Loop exits because m = n = 14. The function returns 14, which is indeed GCD(98, 56) = 14.
Answer: (b) — output matches option (2).
Why not others:
- (a) Option (1) shows 98 56 as the first line, but the output occurs after subtraction, so m is already 42 on the first print.
- (c) Option (3) shows 42 28 on the second line, but n changes from 56 to 14 (not 28) in iteration 2.
- (d) Option (4) shows 56 42 as the first line, which reverses m and n — the output order is m, n, not n, m.
Key rule: Always check whether the output statement is before or after the variable update inside the loop.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.