Count how many numbers from start to end, including both, divide evenly by d.
countMultiples(10, 20, 5) returns 3.
10 strips: A1 to A10
if (i % d == 0) { A1public static int countMultiples(int start, int end, int d) { A2} A3for (int i = start; i <= end; i++) { A4} A5int count = 0; A6return count; A7count++; A8for (int i = start; i < end; i++) { A9} A10Order: A2, A6, A4, A1, A8, A5, A10, A7, A3. Extra: A9
public static int countMultiples(int start, int end, int d) {
int count = 0;
for (int i = start; i <= end; i++) {
if (i % d == 0) {
count++;
}
}
return count;
} Extra strip: for (int i = start; i < end; i++) {. i < end stops before end, so 20 is never checked and the method returns 2.