ProgrammingHardGATE CS · Competitive coding (CP)

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

How many binary strings of length 5 contain no two adjacent 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(5));
}
  1. A. 13
  2. B. 8
  3. C. 21
  4. D. 12

Correct answer: A. 13

f(n) follows the recurrence with f(0) = 1 and f(1) = 2, giving 3, 5, 8 and 13 for n = 2, 3, 4 and 5.

More C Programming questions

Exams that ask this topic