I am trying to do the Sieve of Eratosthenes using a for loop then an if statement by checking for prime numbers up to 30 however I have a problem. Because of my code 2,3 and 5 are all shown as not prime numbers because they are divisible by 2,3 and 5 of course. How do I edit my code to make these come up as prime numbers?
Here is my code
public class prime {
public static void main(String[] args) {
for(int a=2; a<=30; a++){
if(a%2 == 0 || a%3 == 0 || a%5 == 0){
System.out.println(a +" = Not Prime");
}
else
{
System.out.println(a + " = Prime");
}
}
}
}
Thanks
Aucun commentaire:
Enregistrer un commentaire