Java Program to Display Prime Numbers Between Intervals Using Function

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to display prime numbers between intervals using function. Prime numbers are special numbers who have only two factors 1 and itself and cannot be divided by any other number.

A number is a prime number if its only factors are 1 and itself. 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.

Below is a demonstration of the same −

Input

Suppose our input is −

Starting number : 1
Ending number : 75

Output

The desired output would be −

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

Algorithm

Step 1 - START
Step 2 - Declare 2 integer values namely my_high, my_low.
Step 3 - Read the required values from the user/ define the values
Step 4 - Define a function IsPrime which returns Boolean value. The function takes an integer input and checks if the input is divisible by any of its lower number except 1.
Step 5 - If yes, it returns false , else it will return true.
Step 6 - Using a for loop, iterate from my_low to my_high, for each number, call the function IsPrime. If true is returned , it is a prime number, store the number
Step 7 - Display the result
Step 8 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class PrimeNumber {
   public static void main(String[] args) {
      int my_high, my_low;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the starting number : ");
      my_low = my_scanner.nextInt();
      System.out.print("Enter an ending Number: ");
      my_high = my_scanner.nextInt();
      System.out.println("The prime numbers between the interval " + my_low + " and " + my_high + " are:");
      while (my_low < my_high) {
         if (IsPrime(my_low))
            System.out.print(my_low + " ");
            ++my_low;
      }
   }
   public static boolean IsPrime(int my_input) {
      boolean flag = true;
      for (int i = 2; i <= my_input / 2; ++i) {
         if (my_input % i == 0) {
            flag = false;
            break;
         }
      }
      return flag;
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the starting number : 1
Enter the ending number : 75
The prime numbers between the interval 1 and 75 are:
1 2 5 3 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class PrimeNumber {
   public static void main(String[] args) {
      int my_high, my_low;
      my_low = 1;
      my_high = 75;
      System.out.println("The starting and ending numbers are defined as " + my_low + " and " + my_high);
      System.out.println("The prime numbers between the interval " + my_low + " and " + my_high + " are:");
      while (my_low < my_high) {
         if (IsPrime(my_low))
            System.out.print(my_low + " ");
            ++my_low;
      }
   }
   public static boolean IsPrime(int my_input) {
      boolean flag = true;
      for (int i = 2; i <= my_input / 2; ++i) {
         if (my_input % i == 0) {
            flag = false;
            break;
         }
      }
      return flag;
   }
}

Output

The starting and ending numbers are defined as 1 and 75
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
raja
Updated on 22-Feb-2022 07:59:51

Advertisements