ProgrammingVery hardProduct company OA · GATE CS

What is printed?

What is printed?
int calls = 0;
int C(int n, int k) {
    calls++;
    return (k == 0 || k == n) ? 1 : C(n - 1, k - 1) + C(n - 1, k);
}
int main(void) {
    int r = C(6, 3);
    printf("%d %d", r, calls);
}
  1. A. 15 29
  2. B. 20 41
  3. C. 20 39
  4. D. 20 20

Correct answer: C. 20 39

This is Pascal's rule, so C(6, 3) = 20. The recursion tree is binary with every leaf returning 1, so it has 20 leaves and 19 internal nodes: 39 calls. '20 20' counts only the leaves.

More C Programming questions

Exams that ask this topic