Recursion

AP Computer Science A· difficulty 2/5

public static int fact(int n) {
    if (n <= 1) return 1;
    return n * fact(n - 1);
}
System.out.println(fact(4));

What is printed?

  • A

    6

  • B

    24

    check_circle
  • C

    12

  • D

    10

Explanation

fact(4) = 4<em>3</em>2*1 = 24. The base case returns 1 when n <= 1.

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

Related questions