w3resource

Java Project - Prime Number Checker

Efficient Ways to Check for Primes in Java:

Prime Number Checker :

Determine whether a given number is prime or not.

A simple program where the user inputs a number, and the program checks whether the number is prime (a number that can only be divided by 1 and itself).

Input: A number.
Output: Whether the number is prime or not.

Example:

  • Input: 29
  • Output: "Prime"
  • Input: 12
  • Output: "Not prime"

Solution 1: Basic Prime Number Checker in Java

Code:

import java.util.Scanner;

public class PrimeChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);  // Initialize scanner for user input

        // Input the number from the user
        System.out.println("Enter a number: ");
        int number = scanner.nextInt();  

        // Check if the number is prime
        if (isPrime(number)) {
            System.out.println(number + " is a Prime number.");
        } else {
            System.out.println(number + " is Not a Prime number.");
        }

        scanner.close();  // Close the scanner
    }

    // Method to check if a number is prime
    public static boolean isPrime(int num) {
        // Numbers less than 2 are not prime
        if (num < 2) {
            return false;
        }
        // Check divisibility from 2 up to the square root of the number
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;  // If divisible, it's not prime
            }
        }
        return true;  // Otherwise, it's prime
    }
} 

Output:

Enter a number: 
 17
17 is a Prime number.
Enter a number: 
 15
15 is Not a Prime number.

Explanation :

  • Input Handling: The user inputs a number using the Scanner.
  • Prime Check Logic: The method isPrime() checks divisibility of the number starting from 2 up to its square root.
  • Efficiency: Using the square root reduces unnecessary iterations, improving performance for large numbers.
  • Prime Result: Outputs whether the number is prime or not based on the result of the isPrime() method.

Solution 2: Optimized Prime Checker with Edge Case Handling

Code:

import java.util.Scanner;

public class OptimizedPrimeChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);  // Initialize scanner for user input

        // Input number from the user
        System.out.println("Enter a number: ");
        int number = scanner.nextInt();

        // Check if the number is prime
        if (number <= 1) {
            System.out.println(number + " is Not a Prime number.");
        } else if (number == 2 || number == 3) {
            System.out.println(number + " is a Prime number.");  // 2 and 3 are prime
        } else if (number % 2 == 0 || number % 3 == 0) {
            System.out.println(number + " is Not a Prime number.");  // Rule out even numbers and multiples of 3
        } else if (isPrime(number)) {
            System.out.println(number + " is a Prime number.");
        } else {
            System.out.println(number + " is Not a Prime number.");
        }

        scanner.close();  // Close the scanner
    }

    // Optimized method to check if a number is prime
    public static boolean isPrime(int num) {
        // Check from 5 up to the square root of the number, skipping multiples of 2 and 3
        for (int i = 5; i * i <= num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;  // If divisible by i or (i + 2), it's not prime
            }
        }
        return true;  // Otherwise, it's prime
    }
}  

Output:

Enter a number: 
 99
99 is Not a Prime number.
Enter a number: 
 19
19 is a Prime number.

Explanation:

  • Edge Case Handling: Special cases for numbers less than or equal to 1, and small primes like 2 and 3, are handled separately.
  • Optimization: Numbers divisible by 2 or 3 are quickly ruled out before performing more advanced checks.
  • Loop Optimization: The method checks divisibility starting from 5 and skips unnecessary checks by jumping by 6 (since multiples of 2 and 3 are already excluded).
  • Prime Result: Outputs whether the number is prime based on an optimized check.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-prime-number-checker.php