Jump to content

cbox


Recommended Posts

Posted (edited)

I'm in computer science 12 and I'm doing really well but I have a problem on one of my programs is that it's suppose to output the users number saying if it's prime or not prime and I can get prime to work but to display not prime I can't. I had it working before but it was bad coding so I'm fixing it up. My problem is there is a infinate loop and I can't figure it out if anyone can help that would be great.

 

 

//Colin McQueen
//Prime J7
//November 01 09
import java.io.*;//tells Java to use input
class Prime
{//begin class
//Program tells the user if the user's number is a prime number or not
public static void main(String [] args) throws IOException
{//begins main method
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader buffread = new BufferedReader (inStream);
String inData;
int input, remain,denominator;//declares integer variables
boolean prime = true; //declares a boolean variable
System.out.println("Enter a number:");
inData = buffread.readLine();
input = Integer.parseInt(inData);
denominator = input - 1; //denominator is the users number subtract 1
remain = 0;

while (prime = true && denominator > 1)
{//begins loop

remain = (input % denominator); //remain is input mod denominator

if (remain != 0)//remain does not equal 0
{
denominator = denominator - 1; //denominator is denominator - 1
}

else //when remain equals 0
{
prime = false; //denominator equals 0

}

}//ends loop


if (prime = true)//prime equals true and remain doesn't equal 0 or input equals 2
{
System.out.println("Prime");//outputs the number as a Prime
}
else //remain equals 0 or denominator equals 1
{
System.out.println("Not Prime");//outputs the number not a Prime
}

}//end main method
}//end class

 

also some of the comments I have to fix because I didn't bother with them till I get the program 100% working.

Edited by ColinMc
Posted

So you keep breaking the number down, to see if it will equal 0 or not? To check if prime?

 

I think a long time ago when I learned VB, I needed a prime number check for my stat application, to make sure I could divide it or not.

 

I re-install java today, and then check.

Posted (edited)

So you keep breaking the number down, to see if it will equal 0 or not? To check if prime?

 

I think a long time ago when I learned VB, I needed a prime number check for my stat application, to make sure I could divide it or not.

 

I re-install java today, and then check.

 

never mind I got it my mistake was with the if statements for prime had to be prime == true, while I had prime = true. >: ( I'm mad because that was such a small error I couldn't find till today lol.

Edited by ColinMc

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...