- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Java Program to Display Prime Numbers Between Two Intervals

In this article, we will understand how to display prime numbers between two intervals. 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 values namely Step 3 - Read the required values from the user/ define the values Step 4 - Run a while loop between the lower number and the higher number. Step 5 - Run a for loop, iterate over each number between the intervals and check if the number is divisible by any of its lower numbers except 1. Store the values. Step 6 - Display the result Step 7 - 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 .

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

## 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, 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

- Related Articles
- C++ Program to Display Prime Numbers Between Two Intervals
- Swift program to display prime numbers between two intervals
- Java Program to Display Prime Numbers Between Intervals Using Function
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- C program to display the prime numbers in between two intervals
- Golang Program to Display Prime Numbers Between Two Intervals using library functions.
- Swift program to display prime numbers between intervals using function
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Armstrong Numbers Between Intervals Using Function
- C++ Program to Display Armstrong Number Between Two Intervals
- Swift Program to Display Armstrong Number Between Two Intervals
- Swift Program to display Armstrong Numbers Between Intervals Using Function
- Java Program to Display All Prime Numbers from 1 to N
- C program to display all prime numbers between 1 to N using for loop
- Java program to print the Armstrong numbers between two numbers