Recursion

AP Computer Science A· difficulty 2/5

public static int fact(int n) {
  if (n <= 1) return 1;
  return n * fact(n - 1);
}
// call: fact(5)

What does fact(5) return?

  • A

    60

  • B

    24

  • C

    5

  • D

    120

    check_circle

Explanation

5! = 5<em>4</em>3<em>2</em>1 = 120.

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

Related questions