Prime Number Checker in Java

This program checks if a given number is prime. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself.

Java Program


/**
 * This class contains a method to check if a given number is prime.
 */
public class PrimeNumberChecker {

    /**
     * This method checks if the given number is prime.
     * 
     * @param number The number to check.
     * @return true if the number is prime, false otherwise.
     */
    public static boolean isPrime(int number) {
        // Check if number is less than 2, as prime numbers are greater than 1
        if (number < 2) {
            return false;
        }
        // Loop to check if the number has any divisor other than 1 and itself
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                return false; // number is not prime
            }
        }
        return true; // number is prime
    }

    /**
     * Main method to test the isPrime method.
     * 
     * @param args Command line arguments.
     */
    public static void main(String[] args) {
        int numberToCheck = 29; // Example number to check

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

Explanation

The isPrime method works as follows:

  1. First, it checks if the number is less than 2. If so, it returns false because prime numbers are greater than 1.
  2. Next, it loops from 2 to the square root of the number. The reason for checking up to the square root is that if n = a * b, then one of the factors a or b must be less than or equal to the square root of n.
  3. If the number is divisible by any number in this range, it returns false because the number is not prime.
  4. If the loop completes without finding any divisors, the number is prime, and the method returns true.

 

How to Use the Program

  1. Copy the Code: Copy the Java code provided in the <code> tags.
  2. Create a Java File: Create a new file named PrimeNumberChecker.java.
  3. Paste the Code: Paste the copied code into the PrimeNumberChecker.java file.
  4. Compile the Program: Open a terminal or command prompt, navigate to the directory containing the PrimeNumberChecker.java file, and compile the program using the following command:
    javac PrimeNumberChecker.java
  5. Run the Program: After compilation, run the program using the following command:
    java PrimeNumberChecker

Example Output

For the example number 29, the program will output:

29 is a prime number.

You can change the value of numberToCheck in the main method to test other numbers.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)