ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (Pascal's identity for combinations)?

What will be the output of the following pseudocode (Pascal's identity for combinations)?
Function C(Integer n, Integer r)
    if (r == 0 or r == n)
        return 1
    end if
    return C(n - 1, r - 1) + C(n - 1, r)
End Function

Print C(6, 2)
  1. A. 12
  2. B. 15
  3. C. 20
  4. D. 6

Correct answer: B. 15

This is Pascal's triangle recurrence for nCr. C(6,2) = 6!/(2!*4!) = 15, which the recursive breakdown via C(n-1,r-1)+C(n-1,r) also correctly builds up to.

More Pseudocode and Code Tracing questions

Exams that ask this topic