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));
}- A. 13
- B. 8
- C. 21
- 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
- What is printed? c #include <stdio.h> struct item { int w, v; }; int main(void) { struct item items[] = {{1, 10}, {2, 20}, {3, 30}}…
- What is printed? c #include <stdio.h> int main(void) { const char s = "hello"; printf("%c%c", s[1], (s + 4)); }
- What is printed? c #include <stdio.h> int main(void) { int x = 1, y = 2; const int cp = &x; int const pc = &y; pc = 5; printf("%d %d", cp…
- What is printed? Assume double is 8 bytes. c #include <stdio.h> int main(void) { printf("%zu", sizeof(1 ? 'a' : 1.0)); }
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void a, const void b) { return strcmp((const…
- What index is printed when bsearch looks up "cat" in the sorted array {"ant", "bee", "cat", "dog"}? c #include <stdio.h> #include…