I’m trying to display all values that are multiples of 5 in a random array, but the program is printing the entire array. What am I doing wrong?
import java.util.Arrays;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rand = new Random();
int[] myArray = new int[15];
for (int i = 0; i < 15; i++)
{
myArray[i] = rand.nextInt(100);
}
System.out.println("array: " + Arrays.toString(myArray));
System.out.printf("Multiples of 5: ");
for (int i = 0; i<myArray.length; i++)
{
if (myArray[i]%5 == 0);
{
System.out.print(myArray[i] + " " );
}
}
}
}
result:
array: [53, 87, 89, 99, 98, 6, 51, 23, 67, 97, 24, 63, 12, 65, 86]
Multiples of 5: 53 87 89 99 98 6 51 23 67 97 24 63 12 65 86
>Solution :
Delete the semicolon of the if statement in line 17.