65.7k views
3 votes
Write a program that takes three integers as input: low, high, and x. The program then outputs the number of multiples of x between low and high inclusively.

Ex: If the input is:

1 10 2
the output is:

5
Hint: Use the % operator to determine if a number is a multiple of x. Use a for loop to test each number between low and high.

1 Answer

3 votes

public static int countMultiples(int low, int high, int x) {

int count = 0;

for (int i = low; i <= high; i++) {

if (i % x == 0) {

count++;

}

}

return count;

}

User JonLOo
by
7.6k points