Prime Number Program in Java: Efficient Code Examples

Robert Jul 13, 2026

Prime numbers are those natural numbers greater than 1 that have no positive divisors other than 1 and themselves. In the realm of programming, particularly in Java, identifying and working with prime numbers can be an intriguing and essential task, especially in fields like cryptography. In this comprehensive guide, we will explore how to find primes in Java and showcase the power and versatility of this simple yet fundamental concept.

Java code to check whether a given number is prime or not prime number in java NetBeans code
Java code to check whether a given number is prime or not prime number in java NetBeans code

To begin our journey, let's first understand a simple yet efficient way to find prime numbers in Java. We will use the Sieve of Eratosthenes, an ancient algorithm that effectively finds all primes smaller than a given limit.

Java Program to Check Number is Prime or Not | Learn Coding
Java Program to Check Number is Prime or Not | Learn Coding

The Sieve of Eratosthenes in Java

The Sieve of Eratosthenes is an algorithm that works by iteratively marking the multiples of each prime number, starting from 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with a fixed constant difference between them which is equal to that prime.

Java Cheatsheet
Java Cheatsheet

In Java, we can implement this algorithm in a concise and efficient way using boolean arrays. Here's how you can do it.

```java public class SieveOfEratosthenes { public static void sieve(int limit) { boolean[] isPrime = new boolean[limit + 1]; Arrays.fill(isPrime, true); for (int p = 2; p * p <= limit; p++) { if (isPrime[p]) { for (int i = p * p; i <= limit; i += p) { isPrime[i] = false; } } } for (int i = 2; i <= limit; i++) { if (isPrime[i]) { System.out.print(i + " "); } } } public static void main(String[] args) { int n = 30; sieve(n); } } ```

β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€

In this Java implementation, we first initialize all entries in the boolean array as true, indicating that every number is a potential prime. We then iterate through each number, marking its multiples as composite (not prime). The main logic lies in the nested loop that marks the multiples, and the outer loop ensures that we skip multiples of previously marked primes.

Complexity and Efficiency

The time complexity of this algorithm is O(n log log n), which makes it an efficient method for finding all primes up to a given limit. The space complexity is O(n), making it suitable for both small and large inputs.

Program to print first n Prime number
Program to print first n Prime number

However, this algorithm only finds all primes up to a given limit. If you need to check if a specific number is prime, we can use a simpler, iterative method.

Is Prime? Java Implementation

To check if a number is prime, we need to iterate from 2 to the square root of the number and check if any number divides it evenly. If we find such a number, then the input number is not prime. Otherwise, it is prime.

Prime Number Program in Python (Beginner Friendly with Output)
Prime Number Program in Python (Beginner Friendly with Output)

Here's a function in Java that checks if a number is prime using this method:

```java public class PrimeCheck { public static boolean isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } public static void main(String[] args) { int num = 17; if (isPrime(num)) { System.out.println(num + " is a prime number."); } else { System.out.println(num + " is not a prime number."); } } } ```

a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
an image of a computer screen with the text,'check if number is prime '
an image of a computer screen with the text,'check if number is prime '
Java program to check given number is odd, even, or prime using switch case.
Java program to check given number is odd, even, or prime using switch case.
Java Patterns Notes 1
Java Patterns Notes 1
Java Tutorial: Checking Prime Numbers - Easy and Efficient Methods
Java Tutorial: Checking Prime Numbers - Easy and Efficient Methods
Java Basics Cheat Sheet πŸ“˜βš‘ | Learn Coding Faster & Smarter
Java Basics Cheat Sheet πŸ“˜βš‘ | Learn Coding Faster & Smarter
Data Types in Java
Data Types in Java
How to Check if Given Number is Prime in Java - With Example
How to Check if Given Number is Prime in Java - With Example
Java program to find sum of prime numbers from 1 to n - Code for Java c
Java program to find sum of prime numbers from 1 to n - Code for Java c
the info sheet shows how to learn in 30 days
the info sheet shows how to learn in 30 days
Cycle Completed! Prime Python, Java, JS Services: Loop Closed – Order for Infinite Innovations!
Cycle Completed! Prime Python, Java, JS Services: Loop Closed – Order for Infinite Innovations!
Prime Number Program in Java
Prime Number Program in Java
a computer screen with some type of programming program written on the screen and numbers printed on it
a computer screen with some type of programming program written on the screen and numbers printed on it
Top Features of Java Every Beginner Should Know β˜•πŸš€
Top Features of Java Every Beginner Should Know β˜•πŸš€
an image of a computer screen with some text on it and numbers in the background
an image of a computer screen with some text on it and numbers in the background
Program to check whether the number entered by user is prime or not in c++
Program to check whether the number entered by user is prime or not in c++
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
Understanding JDK, JRE, and JVM in Java

If you are starting with Java development, it’s important to understand these three terms:

JDK (Java Development Kit) – Includes JRE, compiler, debugger, and other development tools.
JRE (Java Runtime Environment) – Provides JVM and class libraries required to run applications.
JVM (Java Virtual Machine) – Executes Java bytecode and makes Java platform-independent.

In simple terms:

Developers use JDK to build programs.

Applications run on JRE.

Exe... Class Library, Java Programming, Backend Developer, Java, Programming, Coding
Understanding JDK, JRE, and JVM in Java If you are starting with Java development, it’s important to understand these three terms: JDK (Java Development Kit) – Includes JRE, compiler, debugger, and other development tools. JRE (Java Runtime Environment) – Provides JVM and class libraries required to run applications. JVM (Java Virtual Machine) – Executes Java bytecode and makes Java platform-independent. In simple terms: Developers use JDK to build programs. Applications run on JRE. Exe... Class Library, Java Programming, Backend Developer, Java, Programming, Coding
Logical Operators in Java: Think Smarter, Code Better β˜•πŸš€
Logical Operators in Java: Think Smarter, Code Better β˜•πŸš€

This function iteratively checks if the number n is divisible by any number up to its square root. If it is, then the number is not prime. Otherwise, it's prime.

In conclusion, understanding and implementing prime finding algorithms in Java is a foundational aspect of computer science and programming. It not only introduces you to the basics of number theory but also helps you understand the efficiency and complexity of algorithms. The Sieve of Eratosthenes and the isPrime function are powerful tools in your programming toolbox, essential for a wide range of applications.

So, go ahead and explore the world of primes. Practice and experiment with these algorithms, and remember, mastering the basics is the key to becoming a proficient programmer. Happy coding!