Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Prime Number Program for class

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Prime Number Program for class

    idea here is to have user input an int n and find the prime numbers between 2 and n. then print out how many prime numbers there are for example.. 2-100 = 25 prime numbers. i cant figure out whats wrong with this so far. somehow i get high numbers when i run this program. help pls? .

    public class Prime
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
            int n;
            String choice;
            do
            {
                System.out.print("Enter in value for n: ");
                n = scanner.nextInt();
                int primes = 0;
                for(int i=3; i<=n; i++)
                {
                    for(int j=2; j<i; j++)
                    {
                    if(i%j  == 0){}
                    else{
                        primes = primes +1;}
                    }
                }
                System.out.println("Number of primes: " +primes);
                System.out.println("Do you want to continue? Y/N: ");
                choice = scanner.next();
            }while (choice.equalsIgnoreCase("Y"));
        }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Prime Number Program for class

    You're increasing the prime count whenever a number in question isn't divisible by another number, but you're suppose to only increase the count when the number in question isn't divisible by all other numbers greater than 1 and less than itself (subtle difference here).

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime Number Program for class

    how could i implement that into my code? should i put another if in there??

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Prime Number Program for class

    Adding/removing code willy nilly without understanding what's going on is not a good debugging technique. Examine what your algorithm for checking if a number is prime is actually doing (i.e. work it out as if you were the computer). Try it out with the number 5 (which is prime), as well as with the number 6 (which is not prime). Pay attention to when the prime count increases, then see if you can't figure out a better way to check for what I suggested earlier (hint: you'll probably end up needing another if statement and a boolean flag).

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    chachunga (April 21st, 2011)

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime Number Program for class

    we haven't learned boolean flag. but ill try to do what you said.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Prime Number Program for class

    You haven't learned about booleans yet? Hmm, that seems kind of odd. Booleans are values which can either be true or false.

    boolean isPrime = false; // a boolean variable is set to false
    if(isPrime) // equivalent to if(isPrime == true)
    {
        System.out.println("this will never execute");
    }

    Without booleans, you'd need to use breaks and instead of keeping track of how many numbers are prime, you'd need to keep track of how many numbers are composite. Once you've figured out how many composite numbers there are, you know that the rest of the numbers are prime.

  8. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime Number Program for class

    yah haven't learned booleans yet but learned about breaks, and ill try to get the composite then. thanks for the help.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. my program. Random number generater.
    By james137 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2009, 02:58 PM
  5. Help With Odd/Even number program
    By JonoScho in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 23rd, 2009, 10:53 AM