Java program to check whether number is prime or not

Today we are going to learn java program to check whether number is prime or not.

Prime number program in java

A prime number is a number that is greater than one and divided by itself. Prime numbers are those numbers which cannot be divided by other numbers than 1 or itself.

Also read – java program to display pascal triangle

Example : 2, 3, 5, 7, 11, 13, 17….

In the below example for loop determines if given number is prime or not. In this for loop, condition is checked if number is divisible by any number in range number / 2.

We set “temp” to true and break out of for loop. Hence it is a prime number. If number is not divisible “temp” is false and number is prime number.

public class PrimeNumberInJava
{
   public static void main(String[] args) 
   { 
      int a, b = 0, temp = 0; 
      int number = 66;
      b = number / 2; 
      if(number == 0 || number == 1)
      { 
         System.out.println(number + " is not prime number"); 
      }
      else
      { 
         for(a = 2; a <= b; a++)
         { 
            if(number % a == 0)
            { 
               System.out.println(number + " is not prime number."); 
               temp = 1; 
               break; 
            } 
         } 
         if(temp == 0)
         {
            System.out.println(number + " is prime number.");
         }
      }
   }
}

Output :

java program to check whether number is prime or not


Similarly let’s see java program using while loop,

import java.util.Scanner;

public class JavaPrimeNumber 
{
   public static void main(String[] args) 
   { 
      boolean checkPrime = false;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter any number : "); 
      int number = sc.nextInt();
      int a = 2;
      while(a <= number / 2)
      {
         if(number % a == 0)
         {
            checkPrime = true;
            break;
         }
         ++a;
      }
 
      if(checkPrime)
      {
         System.out.println(number + " is a prime number.");
      }
      else
      {
         System.out.println(number + " is not a prime number.");
      }
      sc.close();
   }
}

Output:

Please enter any number : 50
50 is not a prime number.


Now let’s see java program to check prime number using method,

import java.util.Scanner;

public class JavaPrimes 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in); 
      System.out.print("Please enter a number : "); 
      int num = sc.nextInt(); 
      if(checkPrime(num)) 
      { 
         System.out.println(num + " is a prime number."); 
      }
      else
      { 
         System.out.println(num + " is not a prime number."); 
      }
      sc.close();
   }

   public static boolean checkPrime(int number) 
   { 
      if(number <= 1) 
      { 
         return false; 
      } 
      for(int a = 2; a < Math.sqrt(number); a++) 
      { 
         if(number % a == 0) 
         { 
            return false; 
         } 
      } 
      return true;
   }
}

Output:

Please enter a number : 36
36 is not a prime number.


NOTE: 2 is the only even prime number. 0 and 1 are not prime numbers.