Java Program to Display Prime Numbers Between Intervals Using Function

Java Program to Display Prime Numbers Between Intervals Using Function

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 to Check Whether a Number is Prime or Not.

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 Given Interval :

  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 the required packages
import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    // Scanner class for taking input
    Scanner scn = new Scanner(System.in);
    
    // Input for lower limit
    System.out.print("Enter the lower limit: ");
    int lowerLimit = scn.nextInt();
    
    // Input for upper limit
    System.out.print("Enter the upper limit: ");
    int upperLimit = scn.nextInt();
    
    System.out.print("Prime numbers between " + lowerLimit + " and " + upperLimit + " are:");
    
    // for loop for function calling to check for the prime number
    for (int i = lowerLimit; i <= upperLimit; i++) {
      if (isPrime(i)) {
        System.out.print(i + " ");
      }
    }
  }
  
  public static boolean isPrime(int number){
    // for numbers less than or equal to 1
    if (number <= 1) {
      return false;
    }
    
    // For loop to check whether given number is prime or not
    for (int i = 2; i < number; i++) {
      if (number % i == 0) {
        return false;
      }
    }
    return true;
  }
}

Output:

Enter the lower limit: 10
Enter the upper limit: 20
Prime numbers between 10 and 20 are:11 13 17 19 

Explanation :

The above program first takes input from the user of the lower and upper limits. Then, it uses a for loop to iterate through the numbers between the lower and upper limits and calls the isPrime method to check if each number is prime. If a number is prime, it is printed to the console.

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