# Java Prime Number help



## wolf2009 (Mar 8, 2009)

I need help with making this prime number generator in java for univ HW. I'm using BlueJ for writing and testing the code.

I have made the code, but some logic is wrong. The user has to enter the starting number, 2 for HW, and the ending number. Then the code should display all the prime numbers between the two numbers. And it has to be done with loops and if-else statements.


```
import java.util.Scanner;
public class PrimeNumber
{
    public static void main(String [] args)
    {
        int num1, num2, count, i = 2, prChk = 0 , m = 0;
        Scanner keyIn = new Scanner (System.in);
    
      
        System.out.print("Enter First number : ");
        num1 = keyIn.nextInt();
        
        System.out.print("Enter Second number : ");
        num2 = keyIn.nextInt();
        
        count = num1;
              
        while(count <= num2)
        {
            while(i < count)
            {
                prChk = count % i;
                if(prChk == 0)
                    m = 0;
                else if(prChk == 1)
                    m = 1;
                i++;
            }
            if (m == 0)
                System.out.println(count + ": prime");
            else if(m ==1)
                System.out.println(count + ": not prime");
            count++;
        }
    }
}
```

What am I doing wrong ?


----------



## Disparia (Mar 9, 2009)

From a quick glance, I'd guess it had to to do with your _i_ not being reset in the outer loop. Also, you're evaluating the last value of _m_ from the inner loop (the value may switch several times during the loop). Thirdly, the value of a number modulus another number can be a value other than 0 or 1.


----------



## morpha (Mar 15, 2009)

It seems to be logicaly fine...

My suggestion would be to try putting prChk as a double and changing your if too:
if(prChk == 0.0)
                    m = 0;
else if(prChk == 1.0)
                    m = 1;
else
      whatever....

since modulus can return more than just 0 and 1. and a division can return a decimal number...
sometimes 1 != 1.0


----------



## Oliver_FF (Mar 17, 2009)

This part is logically wrong:

```
while(i < count)
            {
                prChk = count % i;
                if(prChk == 0)
                    m = 0;
                else if(prChk == 1)
                    m = 1;
                i++;
            }
```

You need to write this part of code so that as soon as you find a number that divides exactly then stop. At the moment it will continually overwrite the result of your remainder check until it reaches the last number. Theres a useful keyword "break;" which will drop out of the parent loop:

```
while(i < count)
            {
                prChk = count % i;
                if(prChk == 0) {
                    m = 0;
                    break;
                } else if(prChk == 1)
                    m = 1;
                i++;
            }
```
Now, when if finds a number that divides exactly it will set m to 0 and drop out of the while loop, then print out that this number wasn't prime.


----------



## richardbel (Mar 17, 2009)

i think oliverff is right..


----------



## Oliver_FF (Mar 17, 2009)

Jizzler is also correct, there were two logical errors. The while loop didn't break when it 'failed' and the variables weren't reset after the first iteration.


```
import java.util.Scanner;
public class PrimeNumber
{
    public static void main(String [] args)
    {
        int num1, num2, count, i = 2, prChk = 0 , m = 0;
        Scanner keyIn = new Scanner (System.in);
    
      
        System.out.print("Enter First number : ");
        num1 = keyIn.nextInt();
        
        System.out.print("Enter Second number : ");
        num2 = keyIn.nextInt();
        
        count = num1;
              
        while(count <= num2)
        {
            i=2; m=0;
            while(i < count)
            {
                prChk = count % i;
                if(prChk == 0) {
                    m = 0;
                    break;
                } else if(prChk == 1)
                    m = 1;
                i++;
            }
            if (m == 0)
                System.out.println(count + ": prime");
            else if(m ==1)
                System.out.println(count + ": not prime");
            count++;
        }
    }
}
```


----------

