Nested Iteration

AP Computer Science A· difficulty 3/5

public static int run() {
    int c = 0;
    for (int i = 1; i <= 6; i++) {
        for (int j = 1; j <= 6; j++) {
            if (i % j == 0) c++;
        }
    }
    return c;
}
// Call: System.out.println(run());

What is printed?

  • A

    14

    check_circle
  • B

    12

  • C

    16

  • D

    15

Explanation

Number of divisors of 1..6: 1,2,2,3,2,4 = 14.

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

Related questions