Java Program to Display Prime Numbers Between Two Intervals

Java Program to Display Prime Numbers Between Two Intervals

What is a Prime Number?

A prime number is a positive integer greater than 1 that is only divisible by 1 and itself. It cannot be divided evenly by any other number except 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers, while 4, 6, 8, and 9 are not.

In this Article, we will write a program Display Prime Number Between Two Intervals.

Prime Numbers in Java :

We can check whether a given number is prime or not in java using different methods like loops, Recursion etc. We can also check prime numbers in the given intervals using these methods.

Algorithm to find Prime Number Between two Intervals :

  1. Input the lower limit and upper limit of the interval.
  2. Initialize a loop to iterate through the numbers between the lower limit and upper limit.
  3. For each number in the loop, check if it is a prime number.
  4. If a number is prime, print it.
  5. Continue the loop until the upper limit is reached.

Program to Display Prime Numbers Between Intervals using Function :

Run
// Importing all the required packages
import java.util.Scanner;
import java.util.*;

public class Main{
  public static void main(String[] args){
      
    // Scanner class for taking input
    Scanner scn = new Scanner(System.in);
    System.out.println("Enter the first number: ");
    
    // input for first number
    int num1 = scn.nextInt();
    System.out.println("Enter the second number: ");
    
    // imput for second number
    int num2 = scn.nextInt();
    
    System.out.print("Prime numbers between " + num1 + " and " + num2 + " are: ");
    
    // loop for printing the Numbers between a given range
    for (int i = num1; i <= num2; i++) {
      boolean isPrime = true;
      if (i <= 1) {
        isPrime = false;
      } else {
          
        // loop to check whether a number is prime or not  
        for (int j = 2; j < i; j++) {
          if (i % j == 0) {
            isPrime = false;
            break;
          }
        }
      }
      if (isPrime) {
        System.out.print(i + " ");
      }
    }
    System.out.println();
  }
}
Output:

Enter the first number : 10
Enter the second number : 20
Prime numbers between 10 and 20 are:11 13 17 19 

Explanation :

In the above program, Instead of using a separate isPrime function, the prime number check is done within the for loop that iterates over the range between the two numbers entered by the user. The prime number check uses a similar algorithm as the isPrime function, but is included directly within the main loop.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription