
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Prime Numbers Between Two Intervals in Java
In this article, we will understand how to display prime numbers between two intervals. Prime numbers are special numbers that 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.
Problem Statement
Write a program in Java to display all prime numbers between two given intervals. A prime number is a natural number greater than 1 that is not divisible by any other numbers except 1 and itself. Below is a demonstration of the same ?
Input
Starting number: 1 Ending number: 75
Output
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
Approach 1: Getting Prime Numbers from User Input
In this approach, we prompt the user to enter the starting and ending numbers, and then we iterate through this range to find and display the prime numbers -
- START
- Declare values: my_high, my_low, i, my_temp
- Read values from the user: Prompt user for starting and ending numbers
- Run a while loop: Iterate from my_low to my_high
- Run a for loop: Check if the current number is divisible by any number from 2 to current_number/2
- Display the result: Print the prime numbers found
- STOP
Example
Here, the input is entered by the user based on a prompt. You can try this example live in our coding ground tool -
import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { int my_high, my_low, i; boolean my_temp; 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) { my_temp = false; for(i = 2; i <= my_low/2; ++i) { if(my_low % i == 0) { my_temp = true; break; } } if (!my_temp && my_low != 0 && my_low != 1) System.out.print(my_low + " "); ++my_low; } } }
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
Approach 2: Displaying Prime Numbers for Predefined Range
In this approach, we use predefined starting and ending numbers to find and display the prime numbers within this range -
- START
- Declare values: my_high, my_low, i, my_temp
- Define the values: Set my_low = 1 and my_high = 75
- Run a while loop: Iterate from my_low to my_high
- Run a for loop: Check if the current number is divisible by any number from 2 to current_number/2
- Display the result: Print the prime numbers found
- STOP
Example
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, i; boolean my_temp; 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) { my_temp = false; for(i = 2; i <= my_low/2; ++i) { if(my_low % i == 0) { my_temp = true; break; } } if (!my_temp && my_low != 0 && my_low != 1) System.out.print(my_low + " "); ++my_low; } } }
Output
The starting and ending numbers are defined as 1 and 75 The prime numbers between the interval 1 and 75 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
Conclusion
In this tutorial, we have implemented a Java program to find prime numbers between two intervals involves checking each number to see if it has no divisors other than 1 and itself. This process helps in understanding basic programming concepts like loops and conditionals. The method used is efficient for small ranges but can become slower for larger ranges. It also uses a small, fixed amount of memory, making it a good example of balancing time and space efficiency in programming.