Write a program to find out whether a number is prime or not.
import java.util.Scanner;
public class primeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number : ");
int n = sc.nextInt();
if (isPrime(n)) {
System.out.println(n + " is Prime.");
} else {
System.out.println(n + " is not Prime.");
}
sc.close();
}
public static boolean isPrime(int n) {
for (int i = 2; i <= n / i; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
OUTPUT:
Enter the number :
2
2 is Prime.
Comments
Post a Comment
Please do not enter any spam link in the comment box.