ProgrammingHardTCS Digital · Infosys InfyTQ

What will be the output of the following pseudocode?

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

Correct answer: C. 4 6

The pairs go (48,18), (30,18), (12,18), (12,6), (6,6): 4 subtractions and gcd 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic