# How to check prime number of a given number?

## Check prime number of a given number

Here’s an example Java program to check if a given number is prime or not:

```import java.util.Scanner;

public class PrimeChecker {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
boolean isPrime = true;

if (number < 2) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}
```

The program asks the user to enter a number, and then it checks whether the number is prime or not using a for loop that iterates from 2 to the square root of the number. If the number is divisible by any of the integers in that range, then it is not a prime number. If the number is not divisible by any of the integers in that range, then it is a prime number. The program then prints out a message indicating whether the number is prime or not. 5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%