• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java Prime Number help

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 ?
 
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
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.
 
Joined
Jan 15, 2009
Messages
440 (0.08/day)
Location
Australia
System Name Krogoth
Processor AMD Ryzen 5 5600G @stock
Motherboard Gigabyte B550 AORUS ELITE AX V2
Cooling Stock CPU cooler + 2x Intake 120mm @ 8v + 2x outtake 120mm @8v
Memory G.Skill Ripjaws V 32GB (2x16GB) PC4-28800 DDR4
Video Card(s) MSI Radeon R9 290 Gaming 4GB GDDR5
Storage 1x 4tb WD HDD, 1x1Tb Intel m.2 SSD
Display(s) 2x 27" IPS on swing arms. @UHD
Case Antec Solo II
Audio Device(s) HyoperX Cloud 2 Wireless Headphones
Power Supply I forgot.
Mouse Logitech G302
Keyboard Lenovo legion K500 RGB
Software Windows 11 64bit
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

New Member
Joined
Oct 15, 2006
Messages
544 (0.08/day)
Processor Intel q9400 @ stock
Motherboard Lanparty P45-T2RS
Cooling Zalman CNPS-9500
Memory 8GB OCZ PC2-6400
Video Card(s) BFG Nvidia GTX285 OC
Storage 1TB, 500GB, 500GB
Display(s) 20" Samsung T200HD
Case Antec Mini P180
Audio Device(s) Sound Blaster X-Fi Elite Pro
Power Supply 700w Hiper
Software Ubuntu x64 virtualising Vista
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.
 

Oliver_FF

New Member
Joined
Oct 15, 2006
Messages
544 (0.08/day)
Processor Intel q9400 @ stock
Motherboard Lanparty P45-T2RS
Cooling Zalman CNPS-9500
Memory 8GB OCZ PC2-6400
Video Card(s) BFG Nvidia GTX285 OC
Storage 1TB, 500GB, 500GB
Display(s) 20" Samsung T200HD
Case Antec Mini P180
Audio Device(s) Sound Blaster X-Fi Elite Pro
Power Supply 700w Hiper
Software Ubuntu x64 virtualising Vista
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++;
        }
    }
}
 
Top