ProgrammingEasyTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a = 12, b = 18, t
while (b != 0)
    t = b
    b = a mod b
    a = t
end while
Print a
  1. A. 12
  2. B. 36
  3. C. 6
  4. D. 3

Correct answer: C. 6

This is Euclid's algorithm: (12, 18) → (18, 12) → (12, 6) → (6, 0), so a = gcd(12, 18) = 6. The first iteration just swaps the numbers because 12 mod 18 = 12. lcm would be 36.

More Pseudocode and Code Tracing questions

Exams that ask this topic