for Loops

AP Computer Science A· difficulty 4/5

int a = 24;
int b = 36;
while (a != b) {
  if (a > b) a -= b;
  else b -= a;
}
System.out.println(a);

What is printed?

  • A

    1

  • B

    36

  • C

    12

    check_circle
  • D

    24

Explanation

Subtraction-based GCD: (24,36) -> (24,12) -> (12,12). a == b, exit. Final a = 12, the GCD of 24 and 36.

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

Related questions