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: System.out.println(gcd(48, 36));

What is printed?

  • A

    24

  • B

    4

  • C

    6

  • D

    12

    check_circle

Explanation

gcd(48,36)=gcd(36,12)=gcd(12,0)=12.

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

Related questions