Algorithm
-
Create a Function to Check Prime Numbers:
- Function:
isPrime(num)
- Input:
num
(an integer) - Output:
boolean
(true ifnum
is prime, false otherwise) - Algorithm:
- If
num
is less than or equal to 1, return false. - Iterate from
i
= 2 to the square root ofnum
.- If
num
is divisible evenly byi
, return false.
- If
- If no divisor is found, return true.
- If
- Input:
- Function:
-
Create a Function to Display Prime Numbers in a Range:
- Function:
displayPrimeNumbers(start, end)
- Input:
start
andend
(integers representing the interval) - Output: None (prints prime numbers in the specified range)
- Algorithm:
- Print "Prime numbers between
start
andend
are:" - Iterate from
i
=start
toend
.- If
isPrime(i)
is true, printi
.
- If
- Print "Prime numbers between
- Input:
- Function:
-
Main Method:
- Input: None
- Output: None
- Algorithm:
- Set
intervalStart
to the starting value of the interval. - Set
intervalEnd
to the ending value of the interval. - Call
displayPrimeNumbers(intervalStart, intervalEnd)
.
- Set
Code Examples
#1 Code Example- Prime Numbers Between Two Integers
Code -
Java Programming
public class Prime {
public static void main(String[] args) {
int low = 20, high = 50;
while (low < high) {
if(checkPrimeNumber(low))
System.out.print(low + " ");
++low;
}
}
public static boolean checkPrimeNumber(int num) {
boolean flag = true;
for(int i = 2; i <= num/2; ++i) {
if(num % i == 0> {
flag = false;
break;
}
}
return flag;
}
}
Copy The Code &
Try With Live Editor
Output
23 29 31 37 41 43 47
Demonstration
Java Programing Example to Display Prime Numbers Between Intervals Using Function-DevsEnv