ProgrammingVery hardGATE CS · Competitive coding (CP)

This recursion counts the tilings of a 2 x n board with 2 x 1 dominoes. What is printed?

This recursion counts the tilings of a 2 x n board with 2 x 1 dominoes. What is printed?
#include <stdio.h>
int ways(int n) {
    if (n <= 1) return 1;
    return ways(n - 1) + ways(n - 2);
}
int main(void) {
    printf("%d", ways(6));
}
  1. A. 13
  2. B. 8
  3. C. 21
  4. D. 14

Correct answer: A. 13

ways(n) follows the Fibonacci numbers starting 1, 1, 2, 3, 5, 8, 13, so ways(6) = 13. Using the wrong base case or reading one index too far gives 8, 14 or 21.

More C Programming questions

Exams that ask this topic