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..
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.
Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
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.
Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
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...
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.
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.