ProgrammingVery hardTCS NQT · Accenture

How many ways are there to climb 20 steps taking 1 or 2 at a time? Compute with this exponential recursion.

How many ways are there to climb 20 steps taking 1 or 2 at a time? Compute with this exponential recursion.
#include <stdio.h>
long long w(int n) {
    return n <= 1 ? 1 : w(n - 1) + w(n - 2);
}
int main(void) {
    printf("%lld", w(20));
}
  1. A. 6765
  2. B. 17711
  3. C. 9999
  4. D. 10946

Correct answer: D. 10946

w(n) = w(n - 1) + w(n - 2) with w(0) = w(1) = 1 gives Fibonacci numbers, and w(20) = F(21) = 10946. Starting the base case at w(1) = 0 would give 6765.

More C Programming questions

Exams that ask this topic