ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer Function f(Integer a, Integer b)
    if (a == 0) return b
    return f(b mod a, a)
End Function
Print f(84, 36)
  1. A. 12
  2. B. 36
  3. C. 6
  4. D. 84

Correct answer: A. 12

Calls: f(84, 36) → f(36 mod 84 = 36, 84) → f(84 mod 36 = 12, 36) → f(0, 12) → 12. It is Euclid's gcd with the arguments in the other order; the first call simply swaps them.

More Pseudocode and Code Tracing questions

Exams that ask this topic