Recursion

AP Computer Science A· difficulty 3/5

public static int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
// call: gcd(48, 18)

What is the result?

  • A

    6

    check_circle
  • B

    3

  • C

    18

  • D

    9

Explanation

Euclidean algorithm: gcd(48,18)=gcd(18,12)=gcd(12,6)=gcd(6,0)=6.

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

Related questions