Java program to check whether number is prime or not

Let’s learn java program to check whether number is prime or not.

Java program to check whether number is prime or not

Prime number program in java is one of the most asked interview question.

A prime number is a number that is greater than one and divided by one or itself.

Also read – palindrome number program in java

Prime numbers are those numbers which cannot be divided by other numbers than 1 or itself.

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

Here’s the java program to check whether number is prime or not.

public class PrimeNumberInJava
{
   public static void main(String[] args)
   {
      int a, b = 0, flag = 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.");
               flag = 1;
               break;
            }
         }
         if(flag == 0)
         {
            System.out.println(number + " is prime number.");
         }
      }
   }
}


Output:

java program to check whether number is prime or not

prime number program in java using while loop

Similarly let’s see prime number program in java using while loop.

import java.util.Scanner;
public class PrimeNumberUsingWhileLoop
{
   public static void main(String[] args)
   {
      boolean checkPrime = true;
      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 = false;
            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: 17
17 is a prime number.


print first 10 numbers prime numbers in java

Now let’s see print first 10 prime numbers in java.

public class FirstTenPrimeNumbers
{
   public static void main(String[] args) 
   {
      int count = 0, number = 0, a = 1, b = 1;  
      while(number < 10)  
      {  
         b = 1;  
         count = 0;  
         while(b <= a)  
         {  
            if(a%b == 0)  
                count++;  
            b++;
         }  
         if(count == 2)  
         {  
            System.out.printf("%d ", a);  
            number++;  
         }  
         a++;  
      }
   }
}


Output:

2 3 5 7 11 13 17 19 23 29


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


java program to print prime numbers in a given range

Now let’s learn java program to print prime numbers in a given range.

public class PrimeNumberRangeDemo 
{
   public static void main(String[] args) 
   {
      int lowerRange = 10, higherRange = 50;  
      while(lowerRange < higherRange) 
      {
         boolean flag = false;
         for(int a = 2; a <= lowerRange / 2; ++a) 
         {
            if(lowerRange % a == 0)
            {
               flag = true;
               break;
            }
         }
         if(!flag && lowerRange != 0 && lowerRange != 1)
             System.out.print(lowerRange + " ");
         ++lowerRange;
      }
   }
}


Output:

11 13 17 19 23 29 31 37 41 43 47


print numbers upto n in java

Here let’s learn java program to print prime numbers upto n.

import java.util.Scanner;
public class PrimeUptoN
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      int a = 0;
      int n = 0;
      String strPrime = "";
      System.out.println("Please enter the value of n: ");
      int number = sc.nextInt();
      for(a = 1; a <= number; a++)
      {
         int counter = 0;
         for(n = a; n >= 1; n--)
         {
            if(a % n == 0)
            {
               counter = counter + 1;
            }
         }
         if(counter == 2)
         {
            strPrime = strPrime + a + " ";
         }
      }
      System.out.println("prime numbers upto n are: ");
      System.out.println(strPrime);
      sc.close();
   }
}


Output:

Please enter the value of n: 10
prime numbers upto n are: 2 3 5 7

Please enter the value of n: 50
prime numbers upto n are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47


prime number program in java using scanner

Let’s learn prime number program in java using scanner.

Here’s the java program to check whether number is prime or not.

import java.util.Scanner;
public class PrimeNumberUsingScanner 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);  
      System.out.println("Please enter a number to check prime number: ");  
      int number = sc.nextInt();  
      if(checkPrimeNumber(number)) 
      {  
         System.out.println(number + " is a prime number");  
      }
      else 
      {  
         System.out.println(number + " is not a prime number");  
      }
      sc.close();
   }
   public static boolean checkPrimeNumber(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 to check prime number: 15
15 is not a prime number

Please enter a number to check prime number: 19
19 is a prime number


java program to print prime numbers from 2 to n

Now let’s learn java program to print prime numbers from 2 to n. Here we create checkPrime() method.

In checkPrime() method we have for loop from 2 to number/2.

Here if number divides by some number between 2 to (number/2)-1 then number will not be prime.

Let’s see java program to check whether number is prime or not.

Here’s java program to print prime numbers from 2 to n.

import java.util.Scanner;
public class PrimeNumberFrom2Ton
{
   public static void main(String[] args) 
   {
      int loop, num;
      System.out.println("Please enter value of n: ");
      Scanner sc = new Scanner(System.in);
      num = sc.nextInt();
      for(loop = 2; loop < num; ++loop)
      {
         if(checkPrime(loop))
         {
            System.out.println(loop);
         }
      }
      sc.close();
   }
   public static boolean checkPrime(int number)
   {
      int a;
      boolean boolPrime = true;
      for(a = 2; a < number / 2; a++)
      {
         if(number%a == 0)
         {
            boolPrime = false;
            break;
         }
      }
      return boolPrime;
   }
}


Output:

Please enter value of n: 20
2
3
4
5
7
11
13
17
19

Please enter value of n: 30
2
3
4
5
7
11
13
17
19
23
29