ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int c = 0;
int g(int n) {
    c++;
    if (n <= 2) return 1;
    return g(n - 1) + g(n - 2) + g(n - 3);
}
int main(void) {
    int r = g(6);
    printf("%d %d", r, c);
}
  1. A. 13 25
  2. B. 17 25
  3. C. 17 23
  4. D. 24 25

Correct answer: B. 17 25

Values: g(0)=g(1)=g(2)=1, g(3)=3, g(4)=5, g(5)=9, g(6)=9+5+3=17. Calls: C(n)=1 for n ≤ 2, else 1 + C(n−1) + C(n−2) + C(n−3), giving C(3)=4, C(4)=7, C(5)=13, C(6)=25.

More C Programming questions

Exams that ask this topic