x Turn on thread page Beta
 You are Here: Home >< A-levels

# Java coding help watch

1. my program is intended to check if a number inputted by the user is prime, unfortunately the '!isPrime' part if the while loop doesnt stop the while loop if it finds a factor of the number inputted, and it just carries on.. would love if anyone could tell me why it isnt working . thanks

class Prime{
public static void main(String args[]){

long n;
boolean isPrime=false;
MaInput Ma = new MaInput();
int i=2;

while(!isPrime||i<n){
if(n%i==0){
isPrime=false;
System.out.println(n+" is not prime");
}
else{
isPrime=true;
System.out.println(n+" is prime");
}
i++;
}
}
}
2. (Original post by georgec123)
my program is intended to check if a number inputted by the user is prime, unfortunately the '!isPrime' part if the while loop doesnt stop the while loop if it finds a factor of the number inputted, and it just carries on.. would love if anyone could tell me why it isnt working . thanks

while(!isPrime||i<n){
Shouldn't this be &&, not ||? Otherwise the statement will always be true if i<n.

edit: No, I don't know what you're doing.
3. (Original post by georgec123)
my program is intended to check if a number inputted by the user is prime, unfortunately the '!isPrime' part if the while loop doesnt stop the while loop if it finds a factor of the number inputted, and it just carries on.. would love if anyone could tell me why it isnt working . thanks

class Prime{
public static void main(String args[]){

long n;
boolean isPrime=false;
MaInput Ma = new MaInput();
int i=2;

while(!isPrime||i<n){
if(n%i==0){
isPrime=false;
System.out.println(n+" is not prime";
}
else{
isPrime=true;
System.out.println(n+" is prime";
}
i++;
}
}
}
Your program just finds factors of the input

it should be

int i = 2;
while (i < n) {
if (n % i == 0) { isPrime = true; break; }
else {i++;}
}

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: February 15, 2016
Today on TSR

### Four things top students are doing

Over the Easter break

Poll

## All the essentials

### Student life: what to expect

What it's really like going to uni

### Essay expert

Learn to write like a pro with our ultimate essay guide.

### Create a study plan

Get your head around what you need to do and when with the study planner tool.

### Resources by subject

Everything from mind maps to class notes.

### Study tips from A* students

Students who got top grades in their A-levels share their secrets