ProgrammingHardCapgemini · Infosys pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function countdownSum(Integer n)
    if (n == 0)
        return 0
    end if
    return n + countdownSum(n - 1)
End Function

Print countdownSum(100)
  1. A. 4950
  2. B. 5050
  3. C. 10000
  4. D. 5000

Correct answer: B. 5050

countdownSum(100) recursively unwinds to 100+99+...+1+0, which is the sum of the first 100 positive integers: 100*101/2 = 5050.

More Pseudocode and Code Tracing questions

Exams that ask this topic