ProgrammingHardGATE CS · Competitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
unsigned fib_calls = 0;
int fib(int n) {
    fib_calls++;
    if (n < 2) return n;
    return fib(n - 1) + fib(n - 2);
}
int main(void) {
    fib(10);
    printf("%u", fib_calls);
}
  1. A. 177
  2. B. 176
  3. C. 89
  4. D. 178

Correct answer: A. 177

Each call to fib(n) makes 2 * fib(n + 1) - 1 calls in total, so for n = 10 it is 2 * 89 - 1 = 177. The value 89 is fib(10) + 1, not the call count.

More C Programming questions

Exams that ask this topic