• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Java Prime Number help

  • Thread starter Thread starter wolf2009
  • Start date Start date
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.

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 ?
 
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.
 
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
 
This part is logically wrong:
Code:
            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:
Code:
            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.
 
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.

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)
        {
            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++;
        }
    }
}
 
Back
Top