Checking whether a number is prime should be part of your basic programmers toolkit.
This question makes things a little harder by enclosing the prime checker in a loop which asks if you want to enter another number and repeats if you do. (Section B questions always have nested loops). Using methods makes this sort of code easier to follow.
Note that the isPrime() method here is very basic. This is good enough for the exam. You don't need to waste time optimizing it.
```java
public class Main {
Main(){
Scanner scan = new Scanner(System.in);
boolean onceMore = true;
do {
System.out.println("Enter a number");
int n = scan.nextInt();
if (n<2) System.out.println("Not greater than 1");
if (isPrime(n)) {
System.out.println("Is prime");
} else {
System.out.println("Is not prime");
}
scan.nextLine(); // remove carriage return
System.out.println("Enter another number (y/n)? ");
String s = scan.nextLine();
if (s.equals("n")) onceMore = false;
}while(onceMore);
}
boolean isPrime(int num) {
boolean isPrime = true;
for (int i = 2; i < num; i++) {
if (num % i == 0) isPrime = false;
}
return isPrime;
}
public static void main(String[] args) {
new Main();
}
}
```