Recursion

AP Computer Science A· difficulty 3/5

public static int pow(int b, int e) {
  if (e == 0) return 1;
  int half = pow(b, e / 2);
  if (e % 2 == 0) return half * half;
  return b * half * half;
}
// call: pow(3, 4)

What does pow(3, 4) return?

  • A

    81

    check_circle
  • B

    9

  • C

    27

  • D

    12

Explanation

3^4 = 81 via successive squaring.

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

Related questions