Java Program to Display Prime Numbers Between Two Intervals

Created with Sketch.

Java Program to Display Prime Numbers Between Two Intervals

In this blog post, we will discuss and implement a Java program that displays prime numbers within a specified range of intervals. We’ll provide a detailed explanation of the algorithm used, present the complete Java code, and showcase the program’s output.

Problem Statement:

The objective is to create a Java program that takes two integers as input and outputs all prime numbers within that range (inclusive). Prime numbers are natural numbers greater than 1 that have no positive divisors other than 1 and themselves.

Algorithm:

  1. Input Intervals: Receive two integers representing the lower and upper bounds of the range.

  2. Validate Intervals: Check if the input intervals are valid. Ensure that the lower bound is less than or equal to the upper bound.

  3. Find Prime Numbers: Iterate through the range and identify prime numbers using a function.

  4. Output Prime Numbers: Display the identified prime numbers.

  5. Prime Number Check Function: Create a function to determine if a given number is prime.

Java Program:

import java.util.Scanner;

public class PrimeNumbersInInterval {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            // Get lower and upper bounds as input
            System.out.print("Enter the lower bound of the interval: ");
            int lowerBound = scanner.nextInt();

            System.out.print("Enter the upper bound of the interval: ");
            int upperBound = scanner.nextInt();

            // Validate input intervals
            if (lowerBound <= upperBound) {
                System.out.println("Prime numbers between " + lowerBound + " and " + upperBound + ":");
                displayPrimeNumbers(lowerBound, upperBound);
            } else {
                System.out.println("Invalid input! The lower bound should be less than or equal to the upper bound.");
            }
        } catch (Exception e) {
            System.out.println("Invalid input! Please enter valid integers.");
        } finally {
            scanner.close();
        }
    }

    // Function to check if a number is prime
    private static boolean isPrime(int number) {
        if (number <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

    // Function to display prime numbers in the given interval
    private static void displayPrimeNumbers(int lowerBound, int upperBound) {
        for (int i = lowerBound; i <= upperBound; i++) {
            if (isPrime(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

Output:

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

In this example, the program prompts the user to enter the lower and upper bounds of the interval. It then calculates and displays the prime numbers within that range.

Explanation:

  • The isPrime function checks if a given number is prime. It iterates from 2 to the square root of the number to determine if it has any divisors other than 1 and itself.

  • The displayPrimeNumbers function iterates through the specified interval and calls the isPrime function for each number. If a number is prime, it is displayed.

  • The program handles potential errors, such as invalid input or intervals with the lower bound greater than the upper bound.

Conclusion:

Creating a Java program to display prime numbers in a specified interval involves validating input, implementing a prime-checking function, and iterating through the range. This program can be useful for various applications where prime numbers play a crucial role.

Feel free to run the provided Java program in your Java development environment and experiment with different intervals. If you have any questions or need further clarification, please don’t hesitate to ask!

Leave a Reply

Your email address will not be published. Required fields are marked *