Java Program to Check if a Number is Prime

Introduction

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number cannot be formed by multiplying two smaller natural numbers. This guide will show you how to create a Java program that checks whether a given number is prime.

Problem Statement

Create a Java program that:

  • Takes an integer input from the user.
  • Checks if the number is prime.
  • Returns and displays whether the number is prime or not.

Example 1:

  • Input: 5
  • Output: 5 is a prime number.

Example 2:

  • Input: 10
  • Output: 10 is not a prime number.

Solution Steps

  1. Prompt for Input: Use the Scanner class to read an integer input from the user.
  2. Check if the Number is Less than 2: Any number less than 2 is not prime.
  3. Check Divisibility: Loop through numbers from 2 to the square root of the given number to check if it has any divisors.
  4. Display the Result: Print whether the number is prime or not.

Java Program

Java Program to Check if a Number is Prime

import java.util.Scanner;

/**
 * Java Program to Check if a Number is Prime
 * Author: https://www.javaguides.net/
 */
public class PrimeCheck {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Prompt the user for input
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

        // Step 2: Check if the number is prime
        boolean isPrime = isPrime(number);

        // Step 3: Display the result
        if (isPrime) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }
    }

    // Method to check if a number is prime
    public static boolean isPrime(int number) {
        if (number <= 1) {
            return false; // Numbers less than or equal to 1 are not prime
        }

        // Check divisibility from 2 to the square root of the number
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                return false; // If divisible, the number is not prime
            }
        }
        return true; // If no divisors found, the number is prime
    }
}

Explanation

  • Input: The program prompts the user to enter an integer.

  • Prime Check:

    • The isPrime() method checks if the number is less than or equal to 1 (which is not prime).
    • Then, it checks divisibility starting from 2 up to the square root of the number. This is because if n is divisible by any number greater than its square root, then it must also be divisible by a number smaller than its square root.
    • If any divisor is found, the method returns false, indicating the number is not prime.
    • If no divisors are found, the method returns true, indicating the number is prime.
  • Output: The program prints whether the number is prime or not.

Output Example

Example 1:

Enter a number: 5
5 is a prime number.

Example 2:

Enter a number: 10
10 is not a prime number.

Example 3:

Enter a number: 17
17 is a prime number.

Conclusion

This Java program effectively checks if a number is prime by testing divisibility up to the square root of the number. The program is efficient and handles both small and large numbers appropriately. This method is widely used in programming tasks that involve prime number calculations, such as cryptography, number theory, and algorithm design.

Comments