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