ProgrammingModerateCapgemini · Infosys

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings)
Function f(Integer n)
    if (n < 3)
        return toString(n)
    end if
    return f(n / 3) + toString(n mod 3)
End Function
Print f(100)
  1. A. 20201
  2. B. 1201
  3. C. 10201
  4. D. 10202

Correct answer: C. 10201

This writes n in base 3, most significant digit first: 100 = 81 + 2 × 9 + 1 = 1·3⁴ + 0·3³ + 2·3² + 0·3 + 1, so the output is 10201.

More Pseudocode and Code Tracing questions

Exams that ask this topic