W
wolf2009
Guest
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.
What am I doing wrong ?
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.
Code:
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 ?