### What is prime number

Before check prime number in java program,we should clear what is prime number, a prime number is a number that can only divided by 1 or itself,for example,3,5,7,11,13,17... are prime nimbers,because all of them just can be divided by 1 or itself.

### Check prime number java program.

Always,the logic that check a number whether it is prime number logic or not, is to use the numbers from 2 to （the specific number)/2 to divide the specific number,if the remainder is not 0, than we can say the number is not a prime number.

As the below prime number program in java will show how to check a prime number,first let's check a given number 5 if it's prime number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class PrimeExample{ public static void main(String args[]){ int i,m=0,flag=0; int n=5;//it is the number to be checked m=n/2; if(n==0||n==1){ System.out.println(n+" is not prime number"); }else{ for(i=2;i<=m;i++){ if(n%i==0){ System.out.println(n+" is not prime number"); flag=1; break; } } if(flag==0) { System.out.println(n+" is prime number"); } } } |

output

1 |
5 is prime number |

### Find prime number between to numbers in java program

Sometimes we need to find out the prime numbers between 2 given numbers, so we can use above checking prime number logic to write a checking prime number java program method,and call it to check all the numbers between the two given numbers,let's go.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class PrimeExample{ public static boolean checkPrimeNumber(int num){ int i,m=0; m=num/2; if(num==0||num==1){ return false; // is not prime number }else{ for(i=2;i<=m;i++){ if(num%i==0){ return false; // is not prime number } } return true; // is prime number } } public static void main(String args[]){ //to find out the prime numbers between 10 and 20, and print out the numbers. for(int i=10;i<=20;i++){ if(checkPrimeNumber(i)==true){ System.out.println(i + " is prime number"); } } } } |

output

1 2 3 4 |
11 is prime number 13 is prime number 17 is prime number 19 is prime number |