Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java. Find multiples of 5 in an array

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Delete the semicolon of the if statement in line 17.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading