Java Program to Check Prime Number

PROGRAMMING

3/18/20243 min read

black and silver laptop computer on table
black and silver laptop computer on table

Java Program to Check Prime Number

In Java, a prime number is a number greater than 1 that has no positive divisors other than 1 and itself. We can write a program to check whether a given number is prime or not using the following code:


public class PrimeNumberChecker {
    public static boolean isPrime(int number) {
        if (number <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int number = 29;
        if (isPrime(number)) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }
    }
}

Explanation of the Syntax

The code above uses a method called isPrime to check whether a given number is prime or not. Here's a breakdown of the syntax:

  • public class PrimeNumberChecker: This line declares a public class named PrimeNumberChecker.
  • public static boolean isPrime(int number): This line declares a public static method named isPrime that takes an integer parameter named number and returns a boolean value.
  • if (number <= 1): This line checks if the given number is less than or equal to 1. If it is, the method returns false as prime numbers are greater than 1.
  • for (int i = 2; i <= Math.sqrt(number); i++): This line initializes a for loop that starts from 2 and iterates up to the square root of the given number. It checks if the number is divisible by any number between 2 and its square root. If it is, the method returns false as it is not a prime number.
  • public static void main(String[] args): This line declares the main method, which serves as the entry point of the program.
  • int number = 29: This line declares an integer variable named number and assigns it the value 29. You can change this value to test different numbers.
  • if (isPrime(number)): This line checks if the given number is prime by calling the isPrime method. If it returns true, it prints that the number is prime. Otherwise, it prints that the number is not prime.

Alternative Java Code

Another way to check whether a number is prime is by using the BigInteger class, which provides methods for performing arithmetic operations on large integers. Here's an alternative code snippet:


import java.math.BigInteger;

public class PrimeNumberChecker {
    public static boolean isPrime(BigInteger number) {
        if (number.compareTo(BigInteger.ONE) <= 0) {
            return false;
        }
        for (BigInteger i = BigInteger.valueOf(2); i.compareTo(number.sqrt()) <= 0; i = i.add(BigInteger.ONE)) {
            if (number.mod(i).equals(BigInteger.ZERO)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        BigInteger number = new BigInteger("29");
        if (isPrime(number)) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }
    }
}

Explanation of the Alternative Code

This alternative code uses the BigInteger class to handle large integer values. Here's a breakdown of the syntax:

  • import java.math.BigInteger: This line imports the BigInteger class from the java.math package.
  • public static boolean isPrime(BigInteger number): This line declares a public static method named isPrime that takes a BigInteger parameter named number and returns a boolean value.
  • if (number.compareTo(BigInteger.ONE) <= 0): This line checks if the given number is less than or equal to 1 using the compareTo method of BigInteger. If it is, the method returns false.
  • for (BigInteger i = BigInteger.valueOf(2); i.compareTo(number.sqrt()) <= 0; i = i.add(BigInteger.ONE)): This line initializes a for loop that starts from 2 and iterates up to the square root of the given number using the sqrt method of BigInteger. It checks if the number is divisible by any number between 2 and its square root. If it is, the method returns false.
  • public static void main(String[] args): This line declares the main method, which serves as the entry point of the program.
  • BigInteger number = new BigInteger("29"): This line declares a BigInteger variable named number and assigns it the value 29. You can change this value to test different numbers.
  • if (isPrime(number)): This line checks if the given number is prime by calling the isPrime method. If it returns true, it prints that the number is prime. Otherwise, it prints that the number is not prime.

Real-Life Example and Usage

The prime number checking program can be used in various real-life scenarios, such as:

  • Security systems: Prime numbers play a crucial role in encryption algorithms used in security systems. Checking whether a number is prime is essential for generating secure encryption keys.
  • Mathematical calculations: Prime numbers are used in various mathematical calculations, such as finding the greatest common divisor, generating random numbers, and solving complex equations.
  • Data validation: Prime number checking can be used to validate user input in applications where only prime numbers are allowed, such as generating prime numbers for a mathematical game or puzzle.

By understanding and implementing the prime number checking program, you can enhance your Java programming skills and apply them to solve real-world problems.