Question discussion

What is the output of the following C program?#include int f(int n) { if (n

939Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
int f(int n) {
    if (n <= 1) return 1;
    return f(n - 1) + f(n - 2) + 1;
}
int main() {
    printf("%d", f(5));
    return 0;
}
  • A8
  • B13
  • C15
  • D21
Correct answer: C

Explanation

f(1)=1, f(2)=3, f(3)=5, f(4)=9 and f(5) = 9 + 5 + 1 = 15.

Discussion 0

Nobody has replied yet. Start the discussion!