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));
}- A. 5
- B. 13
- C. 8
- 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
- What is printed? c #include <stdio.h> int main(void) { unsigned char a = 100, b = 200; unsigned char s = (unsigned char)(a + b) / 2…
- How many times does the loop body run? c #include <stdio.h> int main(void) { int c = 0; for (int i = 0, j = 0; i < 4 && j < 4; i++, j +=…
- What is printed? c #include <stdio.h> void inc(int p, int n) { while (n--) (p++)++; } int main(void) { int a[] = {1, 2, 3}; inc(a, 2)…
- What is printed? c #include <stdio.h> int main(void) { unsigned short s = 1; s <<= 15; printf("%d", s); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 'a' < 'b' && 'b' < 'c'); }
- char d[4]; char s[] = "abc"; strncpy(d, s, sizeof d - 1); d[sizeof d - 1] = '0'; printf("%s", d); What is printed?