
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Display Prime Numbers Between Two Intervals
In this article, we will understand how to find prime numbers between two intervals. Prime numbers are special numbers that have only two factors, 1 and itself, and cannot be divided by any other number. 11 is a prime number.
Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers.
Problem Statement
Write a program in Java to display all prime numbers between two given intervals. Below is a demonstration of the same.
Input Starting number: 1 Ending number: 75 Output: The prime numbers between the interval 1 and 75 are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
Finding Prime Numbers Between Two Intervals
Let's discuss the different approaches to find prime numbers between two intervals -
-
Basic iterative approach
-
Using Sieve of Eratosthenes
Basic Iterative Approach
This approach involves individually checking each number in the given range to determine if it is a prime. For iterating, you can use a for loop, a while loop, or a do-while loop. The following are the steps to display prime numbers between intervals.
-
Declare and initialize the start and end of the range.
-
Use a loop for (int num = start; num <= end; num++) to iterate each number from start to end.
-
Assume the number is prime. Use a boolean variable set (isPrime = true).
-
Now, use a loop that will divide the current number by all integers from 2 up to its square root.
-
If divisible (i.e., remainder is 0), isPrime = false, and the number is not prime.
-
If not divisible, then it's a prime number.
Example
In the following program, we are using a for loop to iterate the numbers up to the given interval.
public class PrimeNumbers { public static void main(String[] args) { int start = 10, end = 50; System.out.println("Prime numbers between " + start + " and " + end + " are:"); for (int num = start; num <= end; num++) { if (num <= 1) { continue; } boolean isPrime = true; for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.print(num + " "); } } } }
Following is the output of the above program -
Prime numbers between 10 and 50 are: 11 13 17 19 23 29 31 37 41 43 47
Using Sieve of Eratosthenes
The Sieve of Eratosthenes is an algorithm for finding all prime numbers up to a given limit. Instead of checking each number in the range individually.
This approach uses a boolean array to precompute prime numbers using a boolean array. The following are the steps to display prime numbers between intervals using this approach.
-
We create a boolean array isPrime where each index represents whether the corresponding number is prime.
-
All numbers are initially marked as prime (set to true), except for 0 and 1, which are not prime.
-
Starting from the smallest prime number (2), we iteratively mark all multiples of each prime as false (not prime).
-
After precomputing, the array contains all prime numbers up to the end.
-
Finally, we check or separate and print the prime numbers in the specified range [start, end].
Example
Following is an example of displaying prime numbers between intervals using the Sieve of Eratosthenes -
import java.util.Arrays; public class PrimeNumbersInRange { public static void main(String[] args) { int start = 10, end = 50; System.out.println("Prime numbers between " + start + " and " + end + " are:"); boolean[] isPrime = new boolean[end + 1]; Arrays.fill(isPrime, true); // Assume all numbers are prime initially isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= end; i++) { if (isPrime[i]) { for (int j = i * i; j <= end; j += i) { isPrime[j] = false; // Mark multiples of i as non-prime } } } for (int i = start; i <= end; i++) { if (isPrime[i]) { System.out.print(i + " "); } } } }
Following is the output of the above program -
Prime numbers between 10 and 50 are: 11 13 17 19 23 29 31 37 41 43 47