ITPEC FE Morning April 2022 Question 8

For two non-negative integers x and y, which of the following is the result of the procedure shown in the flowchart below?

Startq0rxr < yYesEndNorr − yqq + 1
OptionValue of qValue of r

Explanation

q is the quotient and r the remainder of x ÷ y — the flowchart divides by repeated subtraction.

  • •Start: q ← 0, r ← x
  • •While r ≥ y: subtract y from r and add 1 to q
  • •Stop when r < y

So q counts how many times y fits into x, and r is what is left, smaller than y. Example with x = 13, y = 4: r goes 13, 9, 5, 1 while q goes 0, 1, 2, 3, ending with q = 3, r = 1, and 13 = 4 × 3 + 1.

Answer (a)

Why not others:

  • •(b) — the dividend is x, because r starts at x, and the divisor is y, the amount subtracted; this reverses them
  • •(c) — swaps the roles: q counts the subtractions, so it is the quotient, and r holds the leftover
  • •(d) — 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.