Recursion

AP Computer Science A· difficulty 4/5

<code>int f(int n) { if (n <= 0) return 0; return n + f(n - 2); } </code></pre> f(5) returns

  • A

    5

  • B

    10

  • C

    9

    check_circle
  • D

    15

Explanation

f(5) = 5 + f(3) = 5 + 3 + f(1) = 5 + 3 + 1 + f(-1) = 9.

Want 10 more like this — adaptive to your weak spots?

Related questions