Prime Number Program in Java Without Function - Efficient Code Example

Robert Jul 13, 2026

Discovering prime numbers, those fascinating natural numbers greater than 1 that have only two positive divisors: 1 and themselves, is an intriguing pursuit in mathematics. Programming this concept in Java without relying on pre-built functions allows you to delve deep into the fundamentals of number theory and coding. Let's embark on this journey to understand prime numbers and how to identify them programmatically in Java.

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 '

Before we dive into the code, let's briefly recap what we mean by prime numbers. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The first six prime numbers are 2, 3, 5, 7, 11, and 13. Notably, 2 is the only even prime number.

Java Program to Print Multiplication Table
Java Program to Print Multiplication Table

Understanding the Prime Number Calculation Logic

To identify prime numbers programmatically, we need to understand the basic logic behind their calculation. A number is considered prime if none of the numbers before it can divide it without a remainder. The largest number that can possibly divide a given number is always the number itself, so we only need to check for divisibility up to the square root of the given number.

an image of a program with numbers and words on the back ground, which reads c + + program to display prime numbers when larger number is entered first
an image of a program with numbers and words on the back ground, which reads c + + program to display prime numbers when larger number is entered first

In Java, you can implement this logic within a simple for loop. The loop iteratively checks each number from 2 to the square root of the given number, and if the number is divisible by any of these, it is not a prime number.

Naive Approach: Checking All Numbers

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

The simplest approach to check if a number is prime is to iterate from 2 to this number, and if any number divides it without a remainder, it is not prime. Here's a simple Java function implementing this:

boolean isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { return false; } } return true; }

However, this approach has a time complexity of O(n), which is inefficient for large numbers. A more optimized approach is to only check divisibility up to the square root of the number, as mentioned earlier.

Java - Add two binary numbers
Java - Add two binary numbers

Optimized Approach: Checking Up To the Square Root

To optimize the prime-checking function, we can reduce the upper limit of our loop to the square root of the given number. This way, we significantly reduce the number of iterations, making the function more efficient. Here's the optimized Java function:

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; }

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 '

Now that we have discussed the logic behind prime number identification, let's see how we can use these functions to list prime numbers within a given range.

Listing Prime Numbers within a Given Range

80+ Pattern Programs In Java
80+ Pattern Programs In Java
an image of a computer screen with text and numbers on it that reads, package programs public class occurrence
an image of a computer screen with text and numbers on it that reads, package programs public class occurrence
Making Sense of Our First Java Program
Making Sense of Our First Java Program
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
Python Program to Check Prime Number
Python Program to Check Prime Number
What are Arrays in Java?
What are Arrays in Java?
Java Cheatsheet
Java Cheatsheet
Java Data Types Explained
Java Data Types Explained
Data Types in Java
Data Types in Java
Top 10 Java Programs, Assignments, and Coding Interview Questions Answers
Top 10 Java Programs, Assignments, and Coding Interview Questions Answers
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program
two different types of numbers that appear to be written in the form of an array
two different types of numbers that appear to be written in the form of an array
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
java at a glance 😎
java at a glance 😎
Topic to Master java Programming
Topic to Master java Programming
Java Basics Cheat Sheet 📘⚡ | Learn Coding Faster & Smarter
Java Basics Cheat Sheet 📘⚡ | Learn Coding Faster & Smarter
some type of text that is in the middle of a table with numbers and symbols on it
some type of text that is in the middle of a table with numbers and symbols on it
a text message that reads,'this javacript function should return true if it is a prime number, and false in all other cases
a text message that reads,'this javacript function should return true if it is a prime number, and false in all other cases

Once you have a function to check if a number is prime, you can use it to list all prime numbers within a given range. Here's a simple Java function that lists all prime numbers from 2 to a given number:

void listPrimes(int n) { for (int i = 2; i <= n; i++) { if (isPrime(i)) { System.out.print(i + " "); } } }

This function uses the previously discussed isPrime function to check each number within the given range. If the number is prime, it is printed to the console.

Listing Primes in a Range using Sieve of Eratosthenes

A more efficient way to list all prime numbers up to a given number is to use the Sieve of Eratosthenes algorithm. This ancient algorithm uses the principle that any number greater than 1 is divisible by a prime number less than or equal to its square root. Here's a Java function implementing this algorithm:

void sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0] = prime[1] = false; for (int p = 2; p * p <= n; p++) { if (prime[p]) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i]) System.out.print(i + " "); } }

The Sieve of Eratosthenes algorithm first initializes all numbers as prime. It then iteratively marks the multiples of each prime number starting from 2, until the square of the current prime number is greater than the given number. Finally, it lists all remaining numbers as primes.

Primarily, programming prime numbers in Java without functions gives you a solid understanding of number theory and algorithm design. The efficiency and elegance of these algorithms, such as the Sieve of Eratosthenes, demonstrate the beauty of mathematics and computer science. With every number you check, you're not only identifying a prime, but you're also learning and growing as a programmer.