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 9 of 9

Thread: Display prime numbers from 100 to 200 in Java

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Display prime numbers from 100 to 200 in Java

    class primeN
     
    {
     
        public static void main(String args[])
     
     
    {
     
            int f=1,j;
            int i;
            for(i=101;i<=199;i+=2)
            //System.out.println(i);
     
     
    {
     
                for(j=3;j<i;j++)
     
     
     
    {
     
                    if(i%j==0)
                        System.out.println(i);
                        f=0;
     
    }
                if(f==1)
                {
                    System.out.println(i);
                    f=1;
     
     
    }
                }
        }
    }



    There are around 21 prime numbers between 100 and 200... but this source code displays only the numbers 101 and 103... can any1 guide me where did i go wrong in this? my knowledge of programming is confined to if,for,do while, switch statements only...


    P.S i am extremely sorry if i have used the wrong section for asking this...


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Display prime numbers from 100 to 200 in Java

     
     
    public class primeN {
     
        public static void main(String[] args) {
            int f=1,j;
            int i;
            for(i=101;i<=199;i+=2)
            //System.out.println(i);
            {
                f=1;
                for(j=3;j<i;j++){
                    if(i%j==0){
                        //System.out.println(i);
                        f=0;
                        break;
                    }
                }
                if(f==1){
                         System.out.println(i);
                         f=1;
                }
     
            }
        }
     
    }

    you forget f=1 statement and break in the for loop
    Last edited by aydea; January 23rd, 2011 at 08:12 AM.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Display prime numbers from 100 to 200 in Java

    aydea bro, thanks a lot.. this program displays exactly 21 prime numbers... i would like to know more about the break statement... what does it actually do? in this example, to which statement does it transfer the control ?
    rep added...

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Display prime numbers from 100 to 200 in Java

    if you want to quit the loop , you use "break". "break" transfer the control at the end of the loop which "break" statement is used.

    for(j=3;j<i;j++){
                    if(i%j==0){
                        //System.out.println(i);
                        f=0;
                        break;
                    }
     }
    //control is here after break

  5. #5
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Display prime numbers from 100 to 200 in Java

    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  6. #6
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Display prime numbers from 100 to 200 in Java

    While what you have now may work, I would make the following changes:

    1. All class names should begin with a capital letter.

    2. The variable f should be a boolean, for readability.

    3. When checking to see if i%j is 0, you only need to check the values of j from 3 to < i/2. This is basic factoring math.

  7. The Following User Says Thank You to DavidFongs For This Useful Post:

    c.P.u1 (January 25th, 2011)

  8. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Display prime numbers from 100 to 200 in Java

    Quote Originally Posted by DavidFongs View Post
    While what you have now may work, I would make the following changes:

    1. All class names should begin with a capital letter.

    2. The variable f should be a boolean, for readability.

    3. When checking to see if i%j is 0, you only need to check the values of j from 3 to < i/2. This is basic factoring math.

    Sorry, but i did not understand the meaning of "f should be a boolean"... though i know the meaning of boolean... cud u plz give me an example...

  9. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display prime numbers from 100 to 200 in Java

    Here's my sample.
    public class PrimeNo {
     
        public static void main(String[] args) {
     
            for(int i=101;i<=199;i++)
            {
            	if(i%2!=0 && i%3!=0 && i%5!=0 && i%7!=0 && i%11!=0){
            		System.out.println(i);
            	}
            }
        }
    }

  10. #9
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Display prime numbers from 100 to 200 in Java

    Quote Originally Posted by c.P.u1 View Post
    Sorry, but i did not understand the meaning of "f should be a boolean"... though i know the meaning of boolean... cud u plz give me an example...
    Think about it this way. Right now you initialize an int variable to 1, then if you determine the number isn't prime, you assign that int variable to 0. Later in the code, if that int variable is 1, you print out the prime number. This is the only thing you are using that int variable for. Wouldn't it make more sense to have a boolean variable named "isPrime" and set it to true, then if you determine the number isn't prime, set it to false. Finally only printing the number out if isPrime is true.

Similar Threads

  1. prime numbers
    By tdz013 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2011, 11:24 AM
  2. composite and prime
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 3
    Last Post: October 13th, 2010, 10:01 PM
  3. Need help.. Counting Prime #'s up to 50 w/while loop
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 6th, 2010, 06:40 PM
  4. Generate prime numbers within fiboncacci series?
    By Manish87 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 7th, 2010, 01:24 PM
  5. How to display ppt slides through java????
    By jj_crazy_1 in forum Java Theory & Questions
    Replies: 0
    Last Post: April 5th, 2010, 11:52 PM