How to account for non-prime numbers 0 and 1 in java?

Posted by shady on Stack Overflow See other posts from Stack Overflow or by shady
Published on 2012-12-13T05:00:43Z Indexed on 2012/12/13 5:03 UTC
Read the original article Hit count: 140

Filed under:
|
|
|
|

I'm not sure if this is the right place to be asking this, but I've been searching for a solution for this on my own for quite some time, so hopefully I've come to the right place.

When calculating prime numbers, the starting number that each number has to be divisible by is 2 to be a non-prime number. In my java program, I want to include all the non-prime numbers in the range from 0 to a certain number, so how do I include 0 and 1? Should I just have separate if and else-if statements for 0 and 1 that state that they are not prime numbers? I think that maybe 0 and 1 should be included in the java for loop, but I don't know how to go about doing that.

for (int i = 2; i < num; i++){
        if (num % i == 0){
            System.out.println(i + " is not a prime number. ");
        }
        else{
             System.out.println(i + " is a prime number. ");
        }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about for-loop