ITPEC FE Morning April 2018 Question 8
What is the result of the procedure in the flowchart below, when x and y are positive integers?
Explanation
s is the quotient and t the remainder of x ÷ y — the flowchart divides by repeated subtraction.
- •Start: s ← 0, t ← x
- •While t ≥ y: subtract y from t and add 1 to s
- •Stop when t < y
So s counts how many times y fits into x, and t is what is left, smaller than y. Example with x = 13, y = 4: t goes 13, 9, 5, 1 while s goes 0, 1, 2, 3, ending with s = 3, t = 1, and 13 = 4 × 3 + 1.
Answer (b)
Why not others:
- •(a) — swaps the roles: s counts the subtractions, so it is the quotient, and t holds the leftover
- •(d) — the dividend is x, because t starts at x, and the divisor is y, the amount subtracted; this reverses them
- •(c) — both reverses the division and swaps quotient and remainder
Key rule: In a repeated-subtraction loop, the counter is the quotient and the value left when the loop stops is the remainder.
Algorithm and Programming · AI-written, may contain errors
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.