Algorithm
-
Input:
- Accept the starting point of the interval (start).
- Accept the ending point of the interval (end).
-
Function isPrime(num):
- If
num
is less than or equal to 1, return false (not prime). - Loop from
i = 2
to the square root ofnum
.- If
num
is divisible evenly byi
, return false (not prime).
- If
- If the loop completes, return true (prime).
- If
-
Function printPrimeNumbers(start, end):
- Iterate from
i = start
toend
.- If
isPrime(i)
returns true, printi
.
- If
- Iterate from
-
Main Program:
- Initialize a Scanner for user input.
- Prompt the user to input the starting point (start) of the interval.
- Prompt the user to input the ending point (end) of the interval.
- Display a message indicating the range of prime numbers to be found.
- Call
printPrimeNumbers(start, end)
to display prime numbers in the specified range.
Code Examples
#1 Code Example- Display Prime Numbers Between two Intervals
Code -
Java Programming
public class Prime {
public static void main(String[] args) {
int low = 20, high = 50;
while (low < high) {
boolean flag = false;
for(int i = 2; i < = low/2; ++i) {
// condition for nonprime number
if(low % i == 0) {
flag = true;
break;
}
}
if (!flag && low != 0 && low != 1)
System.out.print(low + " ");
++low;
}
}
}
Copy The Code &
Try With Live Editor
Output
23 29 31 37 41 43 47
Demonstration
Java Programing Example to Display Prime Numbers Between Two Intervals-DevsEnv