Check Prime Numbers
// Contributed by - Anuj Das ( GC College, Silchar - @ Department of Computer Science )
import java.util.Scanner;
class CheckPrime {
public static void main(String[] args) {
System.out.print("Enter an Integer: ");
int x = new Scanner(System.in).nextInt();
if(x == 1) {
System.out.println(x + " is neither Prime nor Composite!");
return;
}
else {
for(int i=2; i <= x/2; i++) {
if(x%i == 0) {
System.out.println(x + " is not a Prime Number!");
return;
}
}
System.out.println(x + " is a Prime Number!");
}
}
}
OUTPUT
cmd-> javac CheckPrime.java
cmd-> java CheckPrime
Enter an Integer: 15
15 is not a Prime Number!
Enter an Integer: 1
1 is neither Prime nor Composite!
Enter an Integer: 5
5 is a Prime Number!