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

Java Prime Number help

Discussion in 'Programming & Webmastering' started by wolf2009, Mar 8, 2009.

  1. 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 ?
     
  2. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,418 (1.31/day)
    Thanks Received:
    637
    Location:
    Geneva, FL, USA
    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.
     
  3. morpha

    morpha

    Joined:
    Jan 15, 2009
    Messages:
    416 (0.20/day)
    Thanks Received:
    55
    Location:
    Australia
    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
     
  4. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.19/day)
    Thanks Received:
    65
    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.
     
  5. richardbel

    richardbel New Member

    Joined:
    Dec 22, 2008
    Messages:
    261 (0.12/day)
    Thanks Received:
    16
    Location:
    vancouver
    i think oliverff is right..
     
  6. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.19/day)
    Thanks Received:
    65
    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++;
            }
        }
    }
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page