Find Prime Numbers In Array

prime numbers in arrays => Correct way : 


public class q34 {

public static void main(String[] args) {

    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

    int n;

    String primes = "";

    for (int i = 0; i < a.length; i++) {

    int counter = 0;

    for (n = a[i]; n >= 1; n--) {

    if (a[i] % n == 0) {

    counter++;

    }

}

    if (counter == 2) {

    primes = primes + a[i] + " ";

    }

}

    System.out.println(primes);

    }

}


-----------------------------------------------

e.g : a[i]= 3;   0 = 1 count;

    3 2 1

    3 / 3 = 0 , 3 / 2 = 1 , 3 / 1 = 0

    two zero means prime 


Comments