# Trying to generate prime numbers

• 01-17-2013, 12:21 AM
Sulfuri
Trying to generate prime numbers
I started learning java yesterday and I've already gotten into trouble;) I'm trying to make a simple program that generates all prime numbers below 10

My problem is that for some reason the code at the bottom keeps printing out the wrong numbers. If i run the program
it will give me the numbers: 3, 5 , 7 , 9

I'm not sure if this is correct but I think it gives me the 9 because
Code:

` if(i%n != 0)`
Since 9%2 = 1 or in other words not 0 the program thinks "oh ok i'm done here adding a number to "i" and continuing"

Question is how do write the program so that when i = x it checks every number from 2 to x if NONE of these numbers has a remainder that does != 0 it prints it out as a prime number?
So for example if we set i = 5 then it would look like this:

5%2 = 1
5%3 = 2
5%4 = 1

5%x != 0 print as prime

Code:

```        while(i  < 10)           {                                 for(int n = 2; n < i; n++)                 {                                if(i%n != 0) // is prime                         {                                 System.out.println(i + " is a prime");                                 break;                                                         }                         if(i%n == 0)                                 break;                                 }         i++;   }```
• 01-17-2013, 06:00 PM
jhuber151
Re: Trying to generate prime numbers
The issue you are having with the loop is that no matter what the loop will break after the first iteration. To fix this issue you will need to do something along the lines of

Code:

```boolean isPrime = true; while(i  < 10) {       isPrime = true;       for(int n = 2; n < i; n++)       {            if(i%n == 0)           {               isPrime = false;               break;           }       }     if(isPrime)       System.out.println(i + " is a prime");   }  i++;```
• 01-17-2013, 08:50 PM
Sulfuri
Re: Trying to generate prime numbers
Quote:

Originally Posted by jhuber151
The issue you are having with the loop is that no matter what the loop will break after the first iteration. To fix this issue you will need to do something along the lines of

Code:

```boolean isPrime = true; while(i  < 10) {       isPrime = true;       for(int n = 2; n < i; n++)       {            if(i%n == 0)           {               isPrime = false;               break;           }       }     if(isPrime)       System.out.println(i + " is a prime");   }  i++;```

I tried to just copy paste this code into my program and it just keeps printing "2 is a prime"

EDIT: I rewrote the code but i tried with the true and false statements that you suggested, it works now ;)
thanks for help, code below if someones intressted.

Code:

```    boolean IsPrime = true;   while(x < 99)   {           IsPrime = true;           for(Counter = 2;Counter < x;Counter++)           {                   if(x%Counter == 0 && Counter > 1)                   {                           //not a prime                           IsPrime = false;                   }           }                     if(IsPrime)           {                   IsPrime = true;                   System.out.println(x + " is a prime");           }             x++;   }```
• 01-17-2013, 09:39 PM
JosAH
Re: Trying to generate prime numbers
FYI: Counter i always larger than 1 (because you have said so in the header of your for-loop) so you can scratch that test in you if-statement.

kind regards,

Jos
• 01-17-2013, 11:38 PM
jhuber151
Re: Trying to generate prime numbers
That was my fault with it being an infinite loop. I left the i++ outside the brackets of the while loop. If you place the i++ inside the last brackets the code should work fine. ie.
Code:

```boolean isPrime = true; while(i  < 10) {       isPrime = true;       for(int n = 2; n < i; n++)       {           if(i%n == 0)           {               isPrime = false;               break;           }       }       if(isPrime)       System.out.println(i + " is a prime");  i++; }```