Java Program to Check Prime Number

In this post, we will learn how to check whether a number is prime or not using Java Programming language. Let’s see the Java Program to Check Prime Number.

A number is called a Prime number, if it is divisible only by itself and one. This means a Prime number has only two factors – 1 and the number itself. For example: 2, 3, 5, 7, 11, . . . etc.

A number is called a Composite Number if it has more than two factors. For example: 4, 15, 26, 98, . . . etc.

Note: 1 is neither a prime number nor a composite number.

Now, Let’s code a Java Program to check the prime number.

Java Program to Check Prime Number

import java.util.*;
  
  public class Main{
  
  public static void main(String[] args) {
      Scanner scn = new Scanner(System.in);


      //Asking for Input
      System.out.println("Enter the number you want to check : ");
      int number = scn.nextInt();
       
      //logic      
      int count = 0;
      
      for(int i=1; i<=number ; i++){
          if(number % i == 0){
              count++;
          }
      }
      
      if(count == 2){
          System.out.println(""+number+" is a Prime Number !!!");
      }else{
          System.out.println("The given number is not a Prime Number !!!");
      }
  
   }
  }

Output 1

Enter an Number: 7
7 is a Prime Number.

Output 2

Enter an Number: 57
57 is not a Prime Number.

How Does This Program Work ?

<strong>     Scanner scn = new Scanner(System.in);

      //Asking for Input
      System.out.println("Enter the number you want to check : ");
      int number = scn.nextInt();</strong>

In Java, we use the Scanner class to take input. The user is asked to enter a number that he/she wants to check. This number will get stored in the number named variable.

If you want to know more about taking inputs in Java, Read this: Taking input from users in Java

 <strong>   

//logic      
      int count = 0;
      
      for(int i=1; i&lt;=number ; i++){
          if(number % i == 0){
              count++;
          }
      }

</strong>

Now, this program checks for the number of integers, the number is completely divisible. And the value of the number of factors is stored in the count named variable.


if(count == 2){
          System.out.println("The given number is a Prime Number !!!");
      }else{
          System.out.println("The given number is not a Prime Number !!!");
      }

As we all know Prime numbers have only 2 factors i.e. 1 and the number itself. So, if the number contains exactly 2 factors, then the given number is a Prime number otherwise it’s not a Prime number.

Java Program to Check Prime Number[Fast Method]

The above approach explained in Method 1 works fine but it’s slow and can be optimized if we observe the number and its a factor.

We can observe that the factors of the number starts getting repeating itself. So from this we can conclude that if a number is not a prime and has factors other than 1 or number itself then the factor can be found before √number.

By noticing this we can now decrease the search space by √number. Hence our approach now got faster.

Now, let’s code the fast approach to code the Java Program to find prime number.

import java.util.*;

public class Main {

  public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);


    //Asking for Input
    System.out.println("Enter the number you want to check : ");
    int number = scn.nextInt();

    if (number == 2) {
      System.out.println("2 is a Prime Number !!!");
    }
    else {
      int count = 0;
        //logic
      for (int div = 2; div * div <= number ; div++) {
        if (number % div == 0) {
          count++;
        }
      }

      if (count == 0) {
        System.out.println(""+number+" is a Prime Number !!!");
      } else {
        System.out.println(""+number+" is NOT a Prime Number !!!");
      }
    }
    


  }
}

Output 1

Enter an Number: 7
7 is a Prime Number.

Output 2

Enter an Number: 57
57 is not a Prime Number.

How Does This Program Work ?

We only have to check the given number is divisible by any number or not from 2 to √number. So the loop runs from, div = 2 till div * div <= number.

  //logic
      for (int div = 2; div * div &lt;= number ; div++) {
        if (number % div == 0) {
          count++;
        }
      }
 if (count == 0) {
        System.out.println(""+number+" is a Prime Number !!!");
      } else {
        System.out.println(""+number+" is NOT a Prime Number !!!");
      }

So now if the number is divisible by any of the number then the count variable is incremented. Then we can check if the given number is prime or not in an optimised faster way.

The Time Complexity of Method 1 is O( n ) and the Time Complexity of Method 1 is O( √ n )

Conclusion

I hope after going through this post, you understand how to check whether a number is prime or not using Java Programming language and learned to code a Java Program to check Prime Number.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *