A prime number is a number that can only be divided by one and itself. So prime numbers have only two factors: 1 and the number itself. In this post, we’ll look at how to create a java program to Check the given number is a prime number or not.

## Prime Number

A prime number is a number that can only be divided only by 1 and itself. A number is not a prime number if the number is divisible by any another number.

Examples of prime numbers include 2, 3, 5, 7, 11, 13, 17, 19, and so on.

The numbers 0 and 1 are not prime. The only even prime number is 2 because all other even numbers can be divided by 2.

package in.yawin; public class PrimeCheck { public static void main(String[] args) { int value = 11; boolean flag = false; if (value == 0 || value == 1) { System.out.println(value + " is not a prime number."); return; } for (int i = 2; i <= value / 2; ++i) { if (value % i == 0) { flag = true; break; } } if (flag) System.out.println(value + " is not a prime number."); else System.out.println(value + " is a prime number."); } }

### Output

11 is a prime number.

## Explanation

A loop is required to determine whether a given number is prime or not. The loop should loop from 2 to half the number (n/2). If you want to see if 10 is a prime number, you must iterate the loop from 2 to 5. There will be a remainder if you divide the number 10 by the remaining numbers from 6 to 9. As a result, it will not be divisible.

The given number will be divided by the for loop index, which ranges from 2 to n. The given number is divisible by another number if the mod value is 0. As a result, it will not be a prime number. There is no point in checking other numbers if the given number is divisible by any one number. Using the break statement, the for loop will be skipped.