0
0
JavaProgramBeginner · 2 min read

Java Program to Find Prime Numbers in a Range

Use a Java program with a loop from start to end and check each number with a method that tests divisibility by numbers from 2 to its square root to find prime numbers.
📋

Examples

Inputstart = 1, end = 10
Output2 3 5 7
Inputstart = 10, end = 20
Output11 13 17 19
Inputstart = 22, end = 22
Output
🧠

How to Think About It

To find prime numbers in a range, check each number one by one. For each number, test if it is divisible by any number from 2 up to its square root. If it is not divisible by any, it is prime. Collect and print all such numbers.
📐

Algorithm

1
Get the start and end values of the range.
2
For each number from start to end, do:
3
Check if the number is prime by testing divisibility from 2 to its square root.
4
If the number is prime, print it.
5
Repeat until all numbers in the range are checked.
💻

Code

java
public class PrimeRange {
    public static boolean isPrime(int num) {
        if (num <= 1) return false;
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        int start = 1, end = 20;
        for (int i = start; i <= end; i++) {
            if (isPrime(i)) {
                System.out.print(i + " ");
            }
        }
    }
}
Output
2 3 5 7 11 13 17 19
🔍

Dry Run

Let's trace the program for the range 1 to 10.

1

Check number 1

1 is not prime because it is <= 1.

2

Check number 2

2 is prime because no divisor found between 2 and sqrt(2).

3

Check number 3

3 is prime because no divisor found between 2 and sqrt(3).

4

Check number 4

4 is not prime because 4 % 2 == 0.

5

Check number 5

5 is prime because no divisor found between 2 and sqrt(5).

6

Check number 6

6 is not prime because 6 % 2 == 0.

7

Check number 7

7 is prime because no divisor found between 2 and sqrt(7).

8

Check number 8

8 is not prime because 8 % 2 == 0.

9

Check number 9

9 is not prime because 9 % 3 == 0.

10

Check number 10

10 is not prime because 10 % 2 == 0.

NumberIs Prime?
1No
2Yes
3Yes
4No
5Yes
6No
7Yes
8No
9No
10No
💡

Why This Works

Step 1: Check numbers in range

The program loops through each number from start to end to test if it is prime.

Step 2: Prime check logic

For each number, it checks divisibility from 2 up to the square root of the number using num % i == 0. If divisible, it is not prime.

Step 3: Print prime numbers

Numbers that pass the test are printed as prime numbers in the range.

🔄

Alternative Approaches

Using Sieve of Eratosthenes
java
public class PrimeRangeSieve {
    public static void main(String[] args) {
        int start = 1, end = 20;
        boolean[] prime = new boolean[end + 1];
        for (int i = 2; i <= end; i++) prime[i] = true;
        for (int p = 2; p * p <= end; p++) {
            if (prime[p]) {
                for (int i = p * p; i <= end; i += p) {
                    prime[i] = false;
                }
            }
        }
        for (int i = start; i <= end; i++) {
            if (prime[i]) System.out.print(i + " ");
        }
    }
}
This method is faster for large ranges but uses extra memory.
Check divisibility up to num/2
java
public class PrimeRangeHalfCheck {
    public static boolean isPrime(int num) {
        if (num <= 1) return false;
        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        int start = 1, end = 20;
        for (int i = start; i <= end; i++) {
            if (isPrime(i)) System.out.print(i + " ");
        }
    }
}
Simpler but slower than checking up to square root.

Complexity: O(n * sqrt(m)) time, O(1) space

Time Complexity

The program checks each number in the range (n numbers) and for each number checks divisibility up to its square root (sqrt(m)), so total time is O(n * sqrt(m)).

Space Complexity

The program uses constant extra space for variables and no additional data structures, so space complexity is O(1).

Which Approach is Fastest?

The Sieve of Eratosthenes is faster for large ranges but uses extra memory, while the simple divisibility check is easier to understand and uses less memory.

ApproachTimeSpaceBest For
Simple divisibility up to sqrt(n)O(n * sqrt(m))O(1)Small to medium ranges, easy to implement
Sieve of EratosthenesO(n log log n)O(n)Large ranges, faster performance
Divisibility up to n/2O(n * n)O(1)Educational, but inefficient
💡
Check divisibility only up to the square root of the number to improve efficiency.
⚠️
Checking divisibility up to the number itself instead of its square root, causing unnecessary work.