Hi friends, I am trying to solve this problem since two days. I get wrong answer now. Whats is the problem with my code:
public class PrimeNumbers {
public static void main(String[] args) {
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
try
{
int totalCase =Integer.parseInt(r.readLine());
if(totalCase>10)
{
totalCase= 10;
}
for(int i=0;i<totalCase;i++)
{
String[] numbers = r.readLine().split(" ");
int startNo= Integer.parseInt(numbers[0]);
int endNO= Integer.parseInt(numbers[1]);
if (startNo >= 1 && endNO <= 1000000000 && endNO - startNo <= 100000)
{ printPrimeForEachCase(startNo, endNO);
}
else
{
System.out.println("Enter input within limits");
}
}
}
catch(IOException e)
{
System.out.println("Not able to read the inputs");
}
catch(NumberFormatException e)
{
System.out.println("Print proper number format");
}
}
//Check if the number is prime for each testcase
public static void printPrimeForEachCase(int start, int end)
{
for(int i= start;i <= end;i++)
{
if(checkIfPrime(i))
{
System.out.println(i);
}
}
}
//Check if the number is prime
public static boolean checkIfPrime(int n)
{
if(n==2)
{
return true;
}
//all prime numbers except 2 are odd
if(n%2==0)
{
return false;
}
for(int i=3; i<=Math.sqrt(n);i+=2)
{
if(n%i==0)
{
return false;
}
}
return true;
}
}