ProgrammingVery hardGATE CS · Competitive coding (CP)

How many binary strings of length 4 contain no two consecutive 1s? Compute with this recursive program.

How many binary strings of length 4 contain no two consecutive 1s? Compute with this recursive program.
#include <stdio.h>
int f(int n) {
    if (n == 0) return 1;
    if (n == 1) return 2;
    return f(n - 1) + f(n - 2);
}
int main(void) {
    printf("%d", f(4));
}
  1. A. 5
  2. B. 13
  3. C. 8
  4. D. 6

Correct answer: C. 8

f(n) counts valid strings of length n, which obeys the Fibonacci-style recurrence with f(0) = 1 and f(1) = 2. So f(2) = 3, f(3) = 5 and f(4) = 8.

More C Programming questions

Exams that ask this topic