Prime Number is a number greater than 1 and divided by 1 or itself only. Prime number can be divided only by itself or 1 and not by any other number. 0 and 1 are not a Prime Number and 2 is the only even Prime Number.

Example of Prime Number : 2, 3, 5, 7, 11, 13, 17,19,23,etc.

Following Programs illustrate Prime Number Programs in three difference ways.

1) Java Program To check Prime Number using flag

public class PrimeNumberWay1 {
	public static void main(String args[]){    
		  int i,m=0,flag=0;      
		  int n=19;
		  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:

19 is prime number

2) Java Program to check Prime Number using Method

public class PrimeNumberWay2 {

	static void checkPrimeNumber(int n){  
		  int i,m=0,flag=0;      
		  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"); 
			  }  
		  }
		} 
	
	public static void main(String args[]){    
		  checkPrimeNumber(1);  
		  checkPrimeNumber(7);  
		  checkPrimeNumber(13);  
		  checkPrimeNumber(33);  
		}    
} 


Output:

1 is not prime number
7 is prime number
13 is prime number
33 is not prime number

3) Java Program to check Prime Number using boolean

import java.util.Scanner;

public class PrimeNumberWay3 {

	public static void main(String[] args) {  
	       Scanner s = new Scanner(System.in);  
	       System.out.print("Enter a number : ");  
	       int n = s.nextInt();  
	       if (isPrime(n)) {  
	           System.out.println(n + " is a prime number");  
	       } else {  
	           System.out.println(n + " is not a prime number");  
	       }  
	   }  
	  
	   public static boolean isPrime(int n) {  
	       if (n <= 1) {  
	           return false;  
	       }  
	       for (int i = 2; i < Math.sqrt(n); i++) {  
	           if (n % i == 0) {  
	               return false;  
	           }  
	       }  
	       return true;  
	   }  
} 

Output 1 :

Enter a number : 19
19 is a prime number

Output 2 :

Enter a number : 39
39 is not a prime number

Java Program to find Prime Number Between Two Range Of Number

import java.util.Scanner;

public class PrimeNumberWay4 {

	public static void main(String[] args) {  
	       Scanner s = new Scanner(System.in);  
	       System.out.print("Please Enter the first Range : ");  
	       int first_range = s.nextInt();  
	       
	       System.out.print("Please Enter the second Range : ");  
	       int second_range = s.nextInt();  
	       
	       System.out.println("List of all Prime Numbers between " + first_range + " and " + second_range);  
	       
	       for (int i = first_range; i <= second_range; i++) {  
	           if (checkPrime(i)) {  
	               System.out.println(i);  
	           }  
	       }  
	   }  
	   public static boolean checkPrime(int n) {  
	       if (n <= 1) {  
	           return false;  
	       }  
	       for (int i = 2; i <= Math.sqrt(n); i++) {  
	           if (n % i == 0) {  
	               return false;  
	           }  
	       }  
	       return true;  
	   }  
}
  
   
Output:

Please Enter the first Range : 5
Please Enter the second Range : 39
List of all Prime Numbers between 5 and 39
5
7
11
13
17
19
23
29
31
37
Scroll to Top