ProgrammingModerateCapgemini · Infosys pattern

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

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Function fun(Integer n)
    if (n == 0)
        return 0
    end if
    return n mod 10 + fun(n / 10)
End Function

Print fun(9875)
  1. A. 28
  2. B. 5789
  3. C. 2
  4. D. 29

Correct answer: D. 29

fun adds up the digits recursively: 5 + 7 + 8 + 9 = 29. It does not repeat until one digit is left, so 2 (the digital root) is wrong.

More Pseudocode and Code Tracing questions

Exams that ask this topic