Java Program to Display Prime Numbers Between Intervals Using Function
In this comprehensive blog post, we will delve into a Java program designed to identify and display prime numbers within a specified range of intervals. The program emphasizes the use of functions, promoting modularity and readability. Additionally, we will explore the underlying logic and provide a step-by-step explanation.
Introduction
Generating prime numbers within a given range is a fundamental programming task. This Java program leverages the power of functions to encapsulate distinct functionalities, making the code more organized and modular. Users will gain insights into creating efficient, structured programs in Java and understanding the logic behind prime number generation.
Program Logic
The program follows a structured approach to accomplish its goal. Key steps include:
User Input:
- The program prompts the user to enter the lower and upper bounds of the interval.
Prime Number Check:
- A separate function (
isPrime
) is defined to determine whether a given number is prime. - The function iterates from 2 to the square root of the number to check for divisibility.
- A separate function (
Display Prime Numbers:
- Another function (
displayPrimes
) handles the task of displaying prime numbers within the specified range. - It calls the
isPrime
function and prints the prime numbers accordingly.
- Another function (
Main Function:
- The
main
function orchestrates the overall program flow by taking user input and invoking the relevant functions.
- The
Java Program Code
import java.util.Scanner;
public class PrimeNumbersBetweenIntervals {
// Function to check if a number is prime
static boolean isPrime(int num) {
if (num < 2) {
return false; // Not a prime number
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false; // Not a prime number
}
}
return true; // Prime number
}
// Function to display prime numbers in an interval
static void displayPrimes(int lower, int upper) {
System.out.println("Prime numbers between " + lower + " and " + upper + " are:");
for (int i = lower; i <= upper; i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
// Main function
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int lower, upper;
// User input for interval
System.out.print("Enter the lower number of the interval: ");
lower = scanner.nextInt();
System.out.print("Enter the upper number of the interval: ");
upper = scanner.nextInt();
// Display prime numbers
displayPrimes(lower, upper);
}
}
Program Output
Let’s consider an example where the user enters the interval [10, 50]. The program efficiently determines and displays the prime numbers within this range:
Enter the lower number of the interval: 10
Enter the upper number of the interval: 50
Prime numbers between 10 and 50 are:
11
13
17
19
23
29
31
37
41
43
47
Conclusion
This Java program serves as a valuable resource for those looking to understand prime number generation within specified intervals using functions. The modular design enhances code readability and allows for easier maintenance and extension. Readers are encouraged to experiment with different input intervals, explore additional Java concepts, and further optimize or extend the program. Engage with the code, modify inputs, and use it as a foundation for building your Java programming skills. Happy coding!