ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int ways(int n) {
    if (n == 0) return 1;
    if (n < 0) return 0;
    return ways(n - 1) + ways(n - 3);
}
int main(void) {
    printf("%d", ways(7));
}
  1. A. 6
  2. B. 8
  3. C. 9
  4. D. 12

Correct answer: C. 9

ways(n) counts the ways to climb n steps taking 1 or 3 at a time, so ways(n) = ways(n-1) + ways(n-3) with ways(0) = 1. The values run 1, 1, 1, 2, 3, 4, 6, 9, so ways(7) = 9.

More C Programming questions

Exams that ask this topic