Prime Number: The number which is divisible only by
itself and 1 (e.g. 2,3,5,7,11,13,17 etc.)
public class PrimeNum {
public static void main(String[] args) {
Scanner
sc = new Scanner(System.in);
System.out.println("Enter a
number-->");
int num = sc.nextInt();
boolean b = true;
for (int p = 2; p <= num / 2; p++) {
int prime = num % p;
if (prime == 0) {
b = false;
break;
}
}
if (b) {
System.out.println("This is a prime
number.");
}
else {
System.out.println("This is not a
prime number.");
}
}
}
No comments:
Post a Comment