GATE Computer ScienceHardGATE CS (PYQ pattern)

int t(int n) { return n == 0 ? 0 : n % 3 + 10 * t(n / 3); } What is t(97) + t(t(7))?

  1. A. 12311
  2. B. 10142
  3. C. 10331
  4. D. 20222

Correct answer: C. 10331

t writes n in base 3 but reads the digits as a decimal number, least significant digit in the units place. 97 = 81 + 2*9 + 3 + 1 = 10121 in base 3. t(7) = 21 (7 = 2*3 + 1), and t(21): 21 = 2*9 + 1*3 + 0 gives 210. Sum 10121 + 210 = 10331.

More Programming and Data Structures (GATE) questions

Exams that ask this topic