-4
public class Prime {
public static void main(String args[]){
    int x;

    for(x=2; x < 100; x++){
        if((x % 2)> 0){
            System.out.println(x);
        }
    }
}

}

I get 3, 5, 7, 9, 11, and etc with composite numbers instead of 3, 5, 7, 11, and etc with only prime

I just probably can't grasp the idea in coding.

3 Answers3

3
 x % 2 > 0

isn't a condition to check weather the number is prime or not

you need to be unable to divide N with 0 reminder, from 2 to sqrt(N)


jmj
  • 237,923
  • 42
  • 401
  • 438
2

if((x % 2)> 0) is not condition for checking prime. It would given remainder after division, here telling if its odd or even. Being just odd is not condition for prime number as it might have other odd factors too for eg. 21 = 7 * 3 .

Here is simple java code to get all prime numbers between 0 - 100. Explanation mentioned in comments

class Main {
    public static void main(String[] args) {
        for (int n = 2; n <= 100; n++) { // loop for all numbers
            boolean flag = true; // to check if factors other than 1, n
            for (int f = 2; f <= Math.sqrt(n); f++) {
                if (n % f == 0) { // factor found, hence not prime
                    flag = false; // activate flag
                    break; // break
                }
            }
            if (flag) { // if flag is not re set
                System.out.print(n + ", "); // print n
            }
        }
    }
}

Output :

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 

If you are looking for efficiency as well, here is implementation of Sieve of Eratosthenes

class Main {
    public static void main(String[] args) {
        int N = 100;

        // Array to keep record of primes and non primes,
        // 0 - prime, 1 - composite
        int[] arr = new int[N + 1];
        arr[0] = arr[1] = 1;

        // loop from 2 to square root of N
        for (int i = 2; i <= Math.sqrt(N); i++) {

            // check if currently prime
            if (arr[i] == 0) {

                // add all factors of i to composite numbers upto N
                for (int j = i * i; j <= N; j += i) {
                    arr[j] = 1;
                }
            }
        }

        // Print all positions for which fields are not reset
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                System.out.print(i + ", ");
            }
        }
    }
}

Output :

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 
sujithvm
  • 2,351
  • 3
  • 15
  • 16
0

If you just "want the job done" without having to think about the logic behind it, there is a nice "hack" in java you can use to check if a number is prime via Regular Expression:

(Sorry, but I do not remember which Thread I found this in, back when I was researching prime-calculations in java)

//################################################################# isPrimeRegEx #
public boolean isPrime(int n) {
    return !new String(new char[n]).matches(".?|(..+?)\\1+");
}

If you want to learn the logic behind real prime-number-calculation and efficiency I deeply advise you to start by looking into this thread:

Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)

Edit:

In your case you could replace the

if((x % 2)> 0)

which, like most people correctly pointed out, is just doing a modulus (%2 means "remainder of division through 2") and looking if that remainder is bigger than 0 (meaning the expression will be true if x is an odd number), with

if(isPrime(x))
Community
  • 1
  • 1