Answer:
Following are the program in java programming language
import java.io.*; // import package
import java.util.*; // import package for input
class Main
// main class
{
public static int checker(int n)
{
int k=2;
if(n<=1)
return 0; // return 0
if(n==2) // this will return 2
return 1;
while(k<n) // loop
{
if(n%k==0)
return 0; // return 0
k++;
}
return 1; // return 1
}
public static void main(String args[]) // main methid
{
int k1=0,j;// variable declaration
Scanner input1 =new Scanner(System.in); // creating object of scanner class
System.out.print("How many times to test for prime numbers?");
int counting=input1.nextInt(); // read the counting
if(counting<0) // check the condition if countion is less then 0
{
System.out.print("Error! Should be positive.Re-enter: ");
counting=input1.nextInt(); // taking input
}
while(k1<counting)
{
System.out.print("\\Enter lower bound and upper bound:");
int lower=input1.nextInt(); // read the lower bound
int upper=input1.nextInt(); // read the upper bond
if(lower>=upper) // check condition
{
System.out.print("Error! Lower bound should be larger.Reenter: ");
lower=input1.nextInt();
upper=input1.nextInt();
}
for(j=lower;j<=upper;j++)
{
int result=checker(j); // calling functuion checker
if(result==1)
System.out.print(j+" ");
}
k1++;
}
}
}
Output:
How many times to test for prime numbers? 1
Enter lower bound and upper bound: 4
8
5 7
Explanation:
- In this we create a function "checker" of int datatype which return the corresponding prime number .
- In the main, we taking the user input of upper and lower bound and checking the corresponding conditions which are already mentioned in the question.
- Call the checker function in the main method which returns the prime number and finally prints the prime numbers between the lower and upper bound.