ProgrammingVery hardProduct company OA · code-tracing level

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a = 100, b = 7, steps = 0
while (a != b)
    if (a > b)
        a = a - b
    else
        b = b - a
    end if
    steps = steps + 1
end while
Print a, steps
  1. A. 1 14
  2. B. 2 14
  3. C. 1 18
  4. D. 1 17

Correct answer: C. 1 18

Subtraction-based Euclid: the gcd stays the same at every step. 100 − 14 × 7 = 2, so the first 14 steps bring a down to 2 (b = 7). Then b: 7 → 5 → 3 → 1 (3 steps). Then a: 2 → 1 (1 step), and a = b = 1. Steps = 14 + 3 + 1 = 18, gcd = 1. '1 14' counts only the first phase; '1 17' forgets the final step that makes a equal to b.

More Pseudocode and Code Tracing questions

Exams that ask this topic