How to Check if a Number Is Prime in Java

Sheeraz Gul Feb 02, 2024
  1. Use while Loop to Check if a Number Is Prime in Java
  2. Use the for Loop to Check if a Number Is Prime in Java
  3. Create a Method to Check if a Number Is Prime in Java
How to Check if a Number Is Prime in Java

In Java, we can implement different methods to check if a number is prime or not. This tutorial demonstrates different methods of checking if a number is prime or not.

Use while Loop to Check if a Number Is Prime in Java

You can use a while loop to develop a method to check if the input number is prime or not.

Example Code:

package delftstack;
import java.util.Scanner;

public class Is_Prime {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number you want to check: ");
    int Input_Number = sc.nextInt();
    int temp = 2;
    boolean condition = false;
    while (temp <= Input_Number / 2) {
      // condition to check for a non-prime number
      if (Input_Number % temp == 0) {
        condition = true;
        break;
      }
      ++temp;
    }

    if (!condition) {
      System.out.println("The Number " + Input_Number + " is a prime number.");
    } else {
      System.out.println("The Number " + Input_Number + " is not a prime number.");
    }
  }
}

Output:

Enter the number you want to check:
11
The Number 11 is a prime number.

or

Enter the number you want to check:
90
The Number 90 is not a prime number.

Use the for Loop to Check if a Number Is Prime in Java

You can also utilize the for loop to create a method to check if the input number is prime or not.

Example Code:

package delftstack;
import java.util.Scanner;

public class Is_Prime {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number you want to check: ");
    int Input_Number = sc.nextInt();
    boolean condition = false;
    for (int x = 2; x <= Input_Number / 2; ++x) {
      // condition to check for a non-prime number
      if (Input_Number % x == 0) {
        condition = true;
        break;
      }
    }

    if (!condition) {
      System.out.println("The Number " + Input_Number + " is a prime number.");
    } else {
      System.out.println("The Number " + Input_Number + " is not a prime number.");
    }
  }
}

Output:

Enter the number you want to check:
3
The Number 3 is a prime number.

Create a Method to Check if a Number Is Prime in Java

We will create a method named isPrime() and use it to check if the input number is a prime or not.

Example Code:

package delftstack;
import java.util.Scanner;

public class Is_Prime {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number you want to check: ");
    int Input_Number = sc.nextInt();

    if (isPrime(Input_Number)) {
      System.out.println("The Number " + Input_Number + " is a prime number.");
    } else {
      System.out.println("The Number " + Input_Number + " is not a prime number.");
    }
  }

  static boolean isPrime(int input_number) {
    if (input_number <= 1) {
      return false;
    }
    // check for a non-prime number
    for (int x = 2; x < input_number; x++) {
      if (input_number % x == 0) {
        return false;
      }
    }
    return true;
  }
}

Output:

Enter the number you want to check:
10
The Number 10 is not a prime number.
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Number