 # Java Prime Number help

1649 Views 9 Replies 3 Participants Last post by  wolf2009
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.

Code:

Code:
``````import java.util.Scanner;
{
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 ?
See less See more
1 - 10 of 10 Posts
Only ODD numbers can be prime, so we should only be checking those numbers. You also need to break from your inner loop once you realize the number is not a prime. No need to keep checking it, since once a number is not prime, there is no further test that could overrule that decision.

Code:

Code:
``````        count = num1 | 1; // Make sure count is ODD

while(count <= num2)
{
bool prime = true;
i = 2;
while(i < count)
{
//check if count is prime
prChk = count % i;
if(!prChk)
{
prime = false;
break;
}
i++;
}
if (prime) System.out.println(count + ": prime");
else System.out.println(count + ": not prime");
count += 2; // Add to next odd number
}``````
Also, if you are allowed to, a FOR loop is better than a WHILE loop in that instance, as it makes for cleaner code.
See less See more
• 1
I can use for loop. but I'm really a beginner in this class, although I have done some C++ but that is also about basic like classes .

What does this do ?
count = num1 | 1;

also i didn't get this if statement
if(!prChk)

with this if (prime)
don't you mean if(prime = true) ?
Quote:
 What does this do ? count = num1 | 1;
Oring a number by 1 turns any number into an Odd number.

16 | 1 = 17
2,156 | 1 = 2,157
2 | 1 = 3
1 | 1 = 1
7 | 1 = 7

If the person wants to see if the number 20 is Prime, there is no point to check anything, because any Even number can't be a prime number. All Even numbers can be divided by 2. So we should start the check at 21, then add 2 to count to get 23, then 25, skipping all the Even numbers...

Quote:
 also i didn't get this if statement if(!prChk)
if (prChk) is the same as if (prChk == true)
if (!prChk) is the same as if (prChk == false)

They are short cuts and save you from typing. It's also makes for cleaner code.

You could also have just done...

if (!(count % i))
{
}

...and saved yourself needing to type out the variable.
See less See more
This is a bit cleaner code, also uses a FOR loop and increments i by 2 instead of one.

Code:

Code:
``````        for (int count=(num1|1);count<=num2;count+=2)
{
bool prime = true;
for (int i=2;i<count;i+=2)
{
//check if count is prime
if(!(count % i))
{
prime = false;
break;
}
}
if (prime) System.out.println(count + ": prime");
else System.out.println(count + ": not prime");
}``````
See less See more
Quote:
 Originally Posted by wolf2009 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. Code: 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 ?
You also forget to reset i for every time the count loop repeats. Every time the count loop repeats i is not reset so it stays at the previous value of count. So for every repetition in the count loop, the i loop is exicuted only once with i equal to count - 1.

Quote:
 Originally Posted by Kuntz Only ODD numbers can be prime
Well, except for two. Two is the only really odd prime number because its the only one that's even.

(I'll cease with the bad puns now.)
See less See more 2
@Kuntz, the compiler says it can't find the symbol -class bool
Quote:
 Originally Posted by wolf2009 @Kuntz, the compiler says it can't find the symbol -class bool
Change the line that says "bool prime = true" to "boolean prime = true". "bool" is the C++ way of declaring a Boolean variable.
See less See more • 1
A BOOL is just an integer, so you can replace it with one. Some people use BOOL's in their programming, I personally don't, it's up to you. There is no performance difference.

Code:

Code:
``````for (int count=(num1|1);count<=num2;count+=2)
{
int prime = 1;
for (int i=3;i<count;i+=2)
{
if(!(count % i))
{
prime = 0;
break;
}
}
if (prime) System.out.println(count + ": prime");
else System.out.println(count + ": not prime");
}``````
See less See more
Quote:
 Originally Posted by The Bartender Paradox Change the line that says "bool prime = true" to "boolean prime = true". "bool" is the C++ way of declaring a Boolean variable.
that did it
See less See more 1 - 10 of 10 Posts