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

Thread: simple prime or composite numbers

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simple prime or composite numbers

    import java.io.*;
    public class PrimeComposite
    {
      public static void main()throws IOException
       {
        BufferedReader dataIn=new BufferedReader (new InputStreamReader (System.in));
        String ans="", number="";
        int n=0, x=0, counter=0, m=0, j=0;
     
        do
        {
          System.out.print("\f");
          System.out.print("Enter any number:");
          n=Integer.parseInt(dataIn.readLine());
     
          for(x=1; x<=n; x++)
          {
              if(n%x==0)
              {
                counter=counter+1;
                number="prime number!";
     
            }
                if(counter!=2)
                {
                    number="composite number!";
     
              }
     
     
                }
     
     
     
     
     
     
     
             System.out.println(" "+number);
     
     
     
     
            System.out.println("");
          System.out.println("Another try? <y/n>");
          ans=dataIn.readLine();   
        }
         while(ans.equals("Y")|| ans.equals("y"));  
      }
    }






    I DON'T REALLY KNOW WHAT IS WRONG WITH THIS.
    I TRIED CHANGING THINGS WITHIN THIS FOR AT LEAST 20 TIMES, BUT I GOT NO LUCK.
    PLEASE, I'M JUST NEW.. I DON'T KNOW YET ALL ABOUT BOOLEAN AND ALL THE OTHERS,
    ONLY THE SIMPLE INT, DOUBLE AND STRING..
    Last edited by helloworld922; August 20th, 2012 at 11:35 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simple prime or composite numbers

    WHAT IS WRONG WITH THIS.
    Please explain why you think there is something wrong.
    Do you get error messages? Please post the full text of the error message.
    Is the results not what you want? Post the output and explain what is wrong with it and show what you want the output to be.

    Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple prime or composite numbers

    Quote Originally Posted by Norm View Post
    Please explain why you think there is something wrong.
    Do you get error messages? Please post the full text of the error message.
    Is the results not what you want? Post the output and explain what is wrong with it and show what you want the output to be.

    Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting


    This is what I did..

    import java.io.*;
    class PrimeComposite
    {
    public static void main([B]String[] args[/B]) throws IOException
    {
    BufferedReader dataIn=new BufferedReader (new InputStreamReader (System.in));
    String ans="", number="";
    int n=0, x=0, counter=0, m=0, j=0;
     
    do
    {
    System.out.print("\f");
    System.out.print("Enter any number:");
    n=Integer.parseInt(dataIn.readLine());
     
    for(x=1; x<=n; x++)
    {
    if(n%x==0)
    {
    counter=counter+1;
    number="prime number!";
     
    }
    if(counter!=2)
    {
    number="composite number!";
     
    }
     
     
    }

    I've already tried it and it worked.. I'm just a beginner so i can't explain it to you...
    Last edited by helloworld922; August 20th, 2012 at 11:36 AM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: simple prime or composite numbers

    I've already tried it and it worked.. I'm just a beginner so i can't explain it to you...
    @jajabOOm, the JPF forums like to promote a policy of teaching and instruction, rather than spoon-feeding code that 'works' without an explanation as to why. This is outlined in the forum rules as well as the following:
    http://www.javaprogrammingforums.com...n-feeding.html
    Further, this is an almost 6 month old thread...please be careful when resurrecting old threads such as this. Given its age, I am currently opting to not edit your post, however another moderator may feel differently and remove your code (or I may change my stance in the near future) - if this were a more recent thread your code would be removed.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simple prime or composite numbers

    When posting code, please wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple prime or composite numbers

    hey..everything is allright until if(n%x==0)
    all u got to do is:
    for(x=1;x<=n;x++)
    {
    if(n%x==0)
    counter++;
    }
    if(counter==1)
    {
    System.out.println("prime number");
    else
    System.out.println("composite number");
    }
    its more simpler...according to me...
    the mistake was....inside the for loop...u are increasing the counter...
    but u are checking for the prime number before the whole loop executes....
    u can obtain the value of the counter only after it checks for the remainder of each and every number until the input number...
    i think it is the mistake,....i have tried my best to explain...if anything i said was wrong...please correct me....

Similar Threads

  1. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. trying to generate prime numbers
    By yingyang69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 28th, 2011, 12:21 PM
  3. Display prime numbers from 100 to 200 in Java
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 25th, 2011, 03:14 PM
  4. prime numbers
    By tdz013 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2011, 11:24 AM
  5. composite and prime
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 3
    Last Post: October 13th, 2010, 09:01 PM