Java Program to Check Whether a Number is Prime or Not

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Program to Check Whether a Number is Prime or Not

In this we will see multiple ways to check a number is Prime or not.

Prime Number: A prime number is that number which is only divisible by 1 and itself. If the number is divisible by any other number then it is not a prime number.

E.g.

23 is only divisible by 1 and itself, so it is prime number.
45 is divisible by 1, 3, 5, 9, 15, 45, so it is not a prime number.

And our task is to find out different approaches to check whether a number is prime or not. We will see to implement this.

Let’s see different approaches one by one.

Method-I:- Check a number is Prime or not using FOR loop

We can check whether a number is prime or not by using a for loop. We will iterate for loop from 2 to number/2 as number is not divisible more than of itself. In if condition we will check whether number is divisible by any iteration, if yes the number is not prime. If no, then the number is prime number.

Approach

  • Declare and initialize the number to be checked say no.
  • Initialize a variable say temp with 0.
  • Declare a variable say i which will be used in iterations.
  • Take a for loop and iterate i from 2 to no/2.
  • Inside if condition check whether no%i==0, if condition satisfies come out of the loop and print no is not a prime number.
  • If no not divisible by any value of i, then print no is a prime number.

Program:

public class PrimeorNot 
{

  public static void main(String[] args) 
{
    // declare the number to be checked
    int no = 68;
    // initially assign 0 to temp
    int temp = 0;
    // check from 2 to that number, if any found divisible by i 
    // then the number is not a prime
    for (int i = 2; i <= no / 2; ++i) {
        // if number is divisible by any value of i, increment temp value
      if (no % i == 0) {
        temp = temp +1;
        // if any i value found to be divisible simply come out of the loop
        break;
      }
    }
    // if temp is 0, then the number is prime number
    if (temp==0)
      System.out.println(no + " is a prime number.");
    else
      System.out.println(no + " is not a prime number.");
  }
}
Output:

68 is not a prime number.

Method-II:- Check a number is Prime or not using WHILE loop

We can check whether a number is prime or not by using a for loop. Initially we will set a Boolean variable say temp and set it as false.

We will iterate for loop from 2 to number/2 as number is not divisible more than of itself. In if condition we will check whether number is divisible by any iterations, if yes set temp as true.

Now outside a for loop we will take if condition and check whether temp is true. If yes the number is prime number, else the number is not a prime number.

Approach:

  • Declare and initialize number to be checked say no.
  • Declare and initialize a variable say i with 2.
  • Declare a Boolean variable say temp and set it false.
  • Take a while loop and iterate i till no<=2.
  • In if statement check if no%i==0, if yes set temp as true and come out of the loop.
  • Outside while loop, check in if statement, check !temp
  • If value of temp is initially false, then it would be true. Print no is a prime number.
  • Else, print no is not a prime number.

Program :

public class PrimeorNot 
{

  public static void main(String[] args) 
{
    // declare and initialize the number to be checked
    int no = 97, i = 2;
    // set temp to false
    boolean temp = false;
    // iterate from 2 to number/2
    while (i <= no / 2) {
      // check if a number is divisible by 2 to number/2
      if (no % i == 0) {
         // if any number divisible by i, set temp to true 
        temp = true;
        // come out of the loop
        break;
      }
        // increment i value
      ++i;
    }
    // if number not divisible by any value of i, then flag is false
    // if !false i.e. true , then number is a prime number
    if (!temp)
      System.out.println(no + " is a prime number.");
    else
      System.out.println(no + " is not a prime number.");
  }
}
Output:

97 is a prime number.

Method-III: Check whether a number is Prime or not using Scanner class

java.util package package provide a class Scanner. Through which by creating an instance of Scanner class we can ask user to give inputs.

This approach is almost similar to previous methods. In this approach only we have to ask user to input a number. And we can use any of the for loop or while loop as done in previous methods. Let’s see this method below.

Approach:

  • Declare and initialize variable say i with 2.
  • Make instance of Scanner class.
  • Declare a variable say no in which number is to be stored.
  • Through Scanner class ask user to enter a number and store it in no.
  • Declare variable say temp and set to false.
  • Take a while loop and iterate i till no<=2.
  • In if statement check if no%i==0, if yes set temp as true and come out of the loop.
  • Outside while loop, check in if statement check !temp
  • If value of temp is initially false, then it would be true. Print no is a prime number.
  • Else, print no is not a prime number.

Program:

import java.util.Scanner;
public class PrimeorNot 
{
    public static void main(String[] args) 
    {
        int i=2;
        // create instance of scanner class
        Scanner sc = new Scanner(System.in);
        // ask user to enter a number
        System.out.print("Please enter a number: ");
        int no = sc.nextInt();
        // set temp to false
        boolean temp = false;
        // iterate from 2 to number/2
        while (i<= no / 2) 
        {
            // check if a number is divisible by 2 to number/2
            if (no % i == 0) 
            {
                // if any number divisible by i, set temp to true 
                temp = true;
                // come out of the loop
                break;
            }
            // increment i value
            ++i;
        }
        // if number not divisible by any value of i, then flag is false
        // if !false i.e. true , then number is a prime number
        if (!temp)
            System.out.println(no + " is a prime number.");
        else
            System.out.println(no + " is not a prime number.");
    }
}

Output:

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

Method-IV: Check whether a number is Prime or not using Method

We can also check whether a number is prime or not through method call. In the main function we have to just pass the required number and inside the calling function it would be checked whether the number is prime or not.

Approach:

  • Take a user defined method as chePrim which will accept integer argument say no from main function.
  • Declare i, temp and initialize temp as 0.
  • Check if no == 1 or 0, if yes, no is not a prime number.
  • If not satisfied then in else condition implement the following.
  • Take a for loop and iterate i from 2 to no/2.
  • Then inside it, in a if condition check whether no%i==0.
  • If yes, assign 1 to temp, and come out of the loop.
  • Now take another if condition and check if temp=0. If yes, print no is a prime number otherwise print no is not a prime number.

Program:

public class Main
{
    // value of number from main method is passed here as argument
    static void chePrim(int no)
    {
        //declare i and set temp to 0
        int i,temp=0;
        // if no is either 0 or 1 it is not a prime number
        if(no==0||no==1)
        {
            System.out.println(no+" is not a prime number");
        }
        else
        {
            // check from 2 to that number, if any found divisible by i
            // then the number is not a prime
            for(i=2;i<=no/2;i++)
            {
                // if number is divisible by any value of i, increment temp value
                if(no%i==0){
                System.out.println(no+" is not a prime number.");
                temp=1;
                // if any i value found to be divisible simply come out of the loop
                break;
                }
            }
            // if flag is 0, then the number is prime number
            if(temp==0) 
            { 
                System.out.println(no+" is a prime number."); 
            }
            else 
            { 
                System.out.println("The number is not a prime number");
            }
        }
    }
        public static void main(String args[])
        {
        // pass 53 to the calling method
        chePrim(53);
        }
}
Output:

53 is a prime number.

Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.

Related Java Decision Making and Loop Programs: