Prime numbers between an interval calculator – Java Program to Display Prime Numbers Between Two Intervals

Prime numbers between an interval calculator: Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Program to Display Prime Numbers Between Two Intervals

In this article, we will see multiple ways to display prime numbers between two intervals.

In mathematics, prime numbers are the numbers that have only two factors that are 1 and the number itself. Consider the example of number 7, which has only two factors 1 and 7. which means it is a prime number.

Different Approaches.

Let’s see one by one.

Method 1: To find prime numbers between two intervals by using user defined method

We can find prime numbers between two intervals by creating a user defined method. How we can do it, let’s see the below program to understand it.

Explanation:

  • We’ve created a function named checkPrimeNumber() which takes an integer parameter num and returns a boolean value.
  • If the number is prime, it returns true.
  • If not, it returns false.
  • Based on the return value, the number is printed on the screen inside the main().

Program:

public class Prime 
{

    public static void main(String[] args) 
    {
        // two range taken
        // cheking prime numbers in betwen 20 and 50
        int low = 20, high = 50;
        // while loop will execute till 'low' value is less than 'high' value
        // means within interval
        while (low < high) 
        {
            // calling user defined method 'checkPrimeNumber' to check prime number
            if(checkPrimeNumber(low))
                // if that number is a prime number that will be printed
                System.out.print(low + " ");
            // increseaing low value
            ++low;
        }
    }


    // user defined method to check prime number
    public static boolean checkPrimeNumber(int num) 
    {
        boolean flag = true;
        //checking prime number
        for(int i = 2; i <= num/2; ++i) 
        {

            if(num % i == 0) {
                flag = false;
                break;
            }
        }

        return flag;
    }
}
Output :

23 29 31 37 41 43 47

Method 2: To find prime numbers between a given interval using for loop

We can find prime numbers between two intervals by for loops. We can use one for loop to cover the interval and another for loop to check prime numbers.

How we can do it, let’s see the below program to understand it.

Approach :

  • The range of numbers is taken as input and stored in the variables ‘a‘ and ‘b'
  • Then using for-loop, the numbers between the interval of a and b are traversed.
  • For each number in the for loop, it is checked if this number is prime or not. If found prime, print the number.
  • Then the next number in the loop is checked, till all numbers are checked.

Program:

import java.util.Scanner;
 
public class Main 
{

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int a, b, i, j, flag;
        // taking lower bound input
        System.out.printf("Enter lower bound of the interval: ");
        a = sc.nextInt(); 
        // taking higher bound input
        System.out.printf("\nEnter upper bound of the interval: ");
        b = sc.nextInt(); 
        System.out.printf("\nPrime numbers between %d and %d are: ", a, b);
        
        // covering the interval
        for (i = a; i <= b; i++) 
        {

            if (i == 1 || i == 0)
                continue;
            flag = 1;
            
            // checking prime number
            for (j = 2; j <= i / 2; ++j) {
                if (i % j == 0) {
                    flag = 0;
                    break;
                }
            }

            if (flag == 1)
                System.out.println(i);
        }
    }
}
Output:

Enter lower bound of the interval: 5
Enter upper bound of the interval: 50
Prime numbers between 5 and 50 are: 5
7
11
13
17
19
23
29
31
37
41
43
47

Method 3: To find prime numbers using a while loop

We can find prime numbers between two intervals by while loops. We can use one while loop to cover the interval and another while loop to check prime numbers.

How we can do it, let’s see the below program to understand it.

Approach :

  • Input the minimum and maximum value for which you are going to find the prime number.
  • Then using the while-loop, the numbers between the interval are traversed.
  • For each number in the while loop, it is checked if this number is prime or not. If found prime, print the number. Then the next number in the loop is checked, till all numbers are checked.

 Program:

import java.util.Scanner;
public class Main
{
 public static void main(String args[])
 {
    int i,count;
    //scanner class for input
    Scanner sc=new Scanner(System.in);
    //Ask input from user
    System.out.println("Please Enter minimum value : ");
    int minValue=sc.nextInt();
    System.out.println("Please Enter maximum value: ");
    int maxValue=sc.nextInt();
    System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : ");
    //forloop for finding and printing all prime numbers in given range
    i=minValue;
    while(i<=maxValue)
    {
      count=0;
      int j=1;
      while(j<=i)
      {
        if(i%j==0)
        {
          count++;
        }
        j++;
    }
    if(count==2)
      System.out.print(i+" ");
     i++;
    }
 }
}

Output:

Enter lower bound of the interval: 5 
Enter upper bound of the interval: 50 
Prime numbers between 5 and 50 are: 
5 
7 
11 
13 
17 
19 
23 
29
31
37 
41 
43 
47

Related Java Decision Making and Loop Programs: