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 itself. Prime numbers are those numbers which cannot be divided by other numbers than 1 or itself.

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

In the below prime number program in java 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.

Also read – palindrome number program in java

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.

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, 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

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 = 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.


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 = 20;  
      while(lowerRange < higherRange) 
      {
         boolean flag = false;
         for(int a = 2; a <= lowerRange / 2; ++a) 
         {
            if(lowerRange % a == 0)
            {
               flag = true;
               break;
            }
         }
         if(!flag)
             System.out.print(lowerRange + " ");
         ++lowerRange;
      }
   }
}


Output:

11 13 17 19


print numbers upto n

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

import java.util.Scanner;
public class PrimeNumbersUptoN 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);        
      System.out.println("Please enter highest (nth number) number: "); 
      String number = sc.nextLine();
      int num = Integer.parseInt(number); 
      System.out.println("Prime numbers between 1 - " + num); 
      for(int a = 2; a <= num; a++)
      {
         boolean checkPrime = true;
         for(int b = 2; b <= a/2; b++)
         {
            if(a % b == 0)
            {
               checkPrime = false;
               break;
            }
         }
         if(checkPrime == true)
         {
            System.out.println(a);
         }
      }
      sc.close();
   }
}


Output:

Please enter highest (nth number) number: 10
Prime numbers between 1 – 10
2
3
5
7

Please enter highest (nth number) number: 50
Prime numbers between 1 – 50
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 num) 
   {  
      if(num <= 1) 
      {  
         return false;
      }  
      for(int a = 2; a < Math.sqrt(num); a++) 
      {  
         if(num % 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 PrimeNumberExample 
{
   public static void main(String[] args) 
   {
      int a, num;
      System.out.println("Please enter highest (nth number) number: ");
      Scanner sc = new Scanner(System.in);
      num = sc.nextInt();
      for(a = 2; a < num; ++a)
      {
         if(checkPrime(a))
         {
            System.out.println(a);
         }
      }
      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 highest (nth number) number: 20
2
3
4
5
7
11
13
17
19

Please enter highest (nth number) number: 30
2
3
4
5
7
11
13
17
19
23
29