Java ProgramsNumbersPrint Prime Numbers in a Range

Print Prime Numbers in a Range in Java

beginner·  Numbers  ·  Number Theory

Problem

Listing primes in a range means checking every number between two bounds and keeping only the ones that are prime, rather than testing just one candidate.

Given a lower and upper bound, list every prime number found between them.

Input
10, 30
Output
11, 13, 17, 19, 23, 29

Java Program

Java
public class PrintPrimesInRange { static boolean isPrime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; // found a divisor, not prime } return true; } public static void main(String[] args) { int low = 10, high = 30; StringBuilder result = new StringBuilder(); for (int i = low; i <= high; i++) { if (isPrime(i)) { // reuse the same primality check for every candidate if (result.length() > 0) result.append(", "); result.append(i); } } System.out.println(result); } }

Output

11, 13, 17, 19, 23, 29

Core Logic

Testing every number in the range for primality, and collecting the ones that pass, lists every prime between the two bounds.

How It Works
  1. 1isPrime(n) is a helper method testing divisors only up to √n, the standard efficient primality check.
  2. 2The loop tries every i from low to high, inclusive.
  3. 3isPrime(i) checks whether the current number qualifies.
  4. 4Each prime found is appended to the result, separated by commas.
For the range 10 to 30, the scan finds six primes: 11, 13, 17, 19, 23, and 29.
💡

Key Point: Reusing the same isPrime() helper used to check a single number is what makes this a natural extension — listing a range is just calling the same check repeatedly.

Complexity
Time Complexity: O(n√n)Space Complexity: O(n)

Why: Each of the n numbers in the range triggers its own O(√n) primality check, and the result string can grow to hold every prime found.

Key Concepts

helper methodfor loopStringBuilder

Approach 2: Java 8

Java
import java.util.stream.Collectors; import java.util.stream.IntStream; public class PrintPrimesInRangeStream { static boolean isPrime(int n) { if (n < 2) return false; return IntStream.rangeClosed(2, (int) Math.sqrt(n)).noneMatch(i -> n % i == 0); } public static void main(String[] args) { int low = 10, high = 30; // Keeps only the numbers that pass the primality check, then joins them String result = IntStream.rangeClosed(low, high) .filter(PrintPrimesInRangeStream::isPrime) .mapToObj(String::valueOf) .collect(Collectors.joining(", ")); System.out.println(result); } }

Output

11, 13, 17, 19, 23, 29

Core Logic

The same primality check can filter a stream of the range's numbers directly, then join the survivors.

How It Works
  1. 1IntStream.rangeClosed(low, high) generates every number in the range.
  2. 2.filter(...) keeps only the numbers that pass the primality check.
  3. 3.mapToObj(String::valueOf) converts each surviving int into a String.
  4. 4.collect(Collectors.joining(", ")) joins the primes into the final comma-separated result.
Filtering the range 10 to 30 keeps the same six primes the loop version finds, joined identically.
💡

Key Point: The filter condition is the exact same primality check the loop version uses — streams just change how the range is walked and the results collected.

Complexity
Time Complexity: O(n√n)Space Complexity: O(n)

Why: The stream still runs the same O(√n) primality check per number, and Collectors.joining() builds a result string holding every prime found.

Key Concepts

StreamIntStream.rangeClosed()filter()

Related Programs