ProgrammingEasyAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer Function f(Integer n)
    if (n <= 0) return 0
    return (n mod 10) + f(n / 10)     // integer division
End Function
Print f(98765)
  1. A. 5
  2. B. 35
  3. C. 98765
  4. D. 26

Correct answer: B. 35

f adds the last digit and recurses on the rest: 5 + 6 + 7 + 8 + 9 = 35. The recursion depth equals the number of digits.

More Pseudocode and Code Tracing questions

Exams that ask this topic