# simple prime or composite numbers

• February 16th, 2012, 08:55 AM
angelicatomnob
simple prime or composite numbers
Code java:

```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..
• February 16th, 2012, 09:49 AM
Norm
Re: simple prime or composite numbers
Quote:

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.

• August 20th, 2012, 11:07 AM
jajabOOm
Re: simple prime or composite numbers
Quote:

Originally Posted by Norm
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.

This is what I did..

Code java:

```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...
• August 20th, 2012, 11:17 AM
copeg
Re: simple prime or composite numbers
Quote:

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.
• August 20th, 2012, 11:18 AM
Norm
Re: simple prime or composite numbers
[code=java]
[/code]
to get highlighting and preserve formatting.
• August 11th, 2013, 12:02 PM
shreyas_s
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....