Java Program to Check if a Number is Prime

Ramesh Fadatare
2 min readDec 13, 2024

--

Introduction

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Checking if a number is prime is a common task in programming, especially in algorithms related to number theory. This guide will walk you through writing a Java program that checks if a given number is prime.

Problem Statement

Create a Java program that:

  • Prompts the user to enter a number.
  • Checks if the number is a prime number.
  • Displays whether the number is prime or not.

Example:

  • Input: 17
  • Output: "17 is a prime number."
  • Input: 18
  • Output: "18 is not a prime number."

Solution Steps

  1. Read the Number: Use the Scanner class to take the number as input from the user.
  2. Check if the Number is Prime: Implement logic to determine if the number is prime.
  3. Display the Result: Print whether the number is prime or not.
  4. Close Resources: Close the Scanner class object automatically using the try-resource statement.

Java Program

// Java Program to Check if a Number is Prime

import java.util.Scanner;

public class PrimeNumberChecker {
public static void main(String[] args) {
// Step 1: Read the number from the user
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Step 2: Check if the number is prime
boolean isPrime = checkPrime(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 checkPrime(int number) {
// Prime numbers are greater than 1
if (number <= 1) {
return false;
}
// 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;
}
}
return true;
}
}

Explanation

Step 1: Read the Number

  • The Scanner class is used to read an integer input from the user. The nextInt() method captures the input number.

Step 2: Check if the Number is Prime

  • The checkPrime() method is used to determine if the number is prime.
  • Numbers less than or equal to 1 are not prime.
  • For numbers greater than 1, the method checks if there are any divisors other than 1 and the number itself by testing divisibility from 2 up to the square root of the number. If a divisor is found, the number is not prime.

Step 3: Display the Result

  • The program prints whether the number is prime based on the result returned by the checkPrime() method.

Output 1

Enter a number: 17
17 is a prime number.

Output 2

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

Conclusion

This Java program demonstrates how to check if a given number is prime. It covers essential concepts such as loops, conditionals, and mathematical operations, making it a valuable exercise for beginners learning Java programming.

--

--

No responses yet