Recursion

AP Computer Science A· difficulty 4/5

public static int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
System.out.println(gcd(24, 18));

What is printed?

  • A

    12

  • B

    3

  • C

    6

    check_circle
  • D

    2

Explanation

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

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

Related questions