Java Program for Prime Numbers Within a Range

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Introduction

Prime numbers are natural numbers greater than 1 that have no divisors other than 1 and themselves. Finding prime numbers within a specific range is a common problem in programming. This guide will show you how to create a Java program that finds all prime numbers within a specified range.

Problem Statement

Create a Java program that:

  • Takes two integer inputs, start and end, from the user.
  • Finds and displays all prime numbers within the range [start, end].

Example 1:

  • Input: start = 10, end = 20
  • Output: Prime numbers: 11, 13, 17, 19

Example 2:

  • Input: start = 50, end = 100
  • Output: Prime numbers: 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

Solution Steps

  1. Prompt for Input: Use the Scanner class to read two integer inputs from the user: start and end.
  2. Check Each Number for Primality: Create a method to determine if a number is prime by checking if it is divisible by any number other than 1 and itself.
  3. Loop Through the Range: Use a loop to check each number within the range [start, end] to see if it is prime.
  4. Display the Prime Numbers: Print the prime numbers found within the specified range.

Java Program

import java.util.Scanner; /** * Java Program to Find Prime Numbers Within a Range * Author: https://www.javaguides.net/ */ public class PrimeNumbersInRange { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt the user for input System.out.print("Enter the start of the range: "); int start = scanner.nextInt(); System.out.print("Enter the end of the range: "); int end = scanner.nextInt(); // Step 2: Find and display prime numbers within the range System.out.print("Prime numbers between " + start + " and " + end + ": "); for (int i = start; i <= end; i++) { if (isPrime(i)) { System.out.print(i + " "); } } System.out.println(); } // Step 3: Method to check if a number is prime public 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; } } 

Explanation

Step 1: Prompt for Input

  • The program prompts the user to enter the start and end of the range. These values define the range [start, end] within which the program will search for prime numbers.

Step 2: Find and Display Prime Numbers

  • The program iterates through all numbers in the specified range [start, end]. For each number, it checks if the number is prime by calling the isPrime method.

Step 3: Method to Check If a Number Is Prime

  • The isPrime method determines if a number is prime by checking if it is divisible by any number other than 1 and itself. The loop in this method runs up to the square root of the number (Math.sqrt(number)) to improve efficiency.

Key Points:

  • Prime Number: A number that has only two distinct positive divisors: 1 and itself.
  • Efficiency: The isPrime method checks divisibility up to the square root of the number, reducing the number of iterations and making the program more efficient.

Output Examples

Example 1:

Enter the start of the range: 10 Enter the end of the range: 20 Prime numbers between 10 and 20: 11 13 17 19 

Example 2:

Enter the start of the range: 50 Enter the end of the range: 100 Prime numbers between 50 and 100: 53 59 61 67 71 73 79 83 89 97 

Conclusion

This Java program efficiently finds all prime numbers within a specified range [start, end]. By using a method that checks for prime numbers and iterating through the given range, the program is both straightforward and efficient. This method is commonly used in various mathematical and computational applications where prime numbers are needed within a certain range.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare