1 / 3
Feb 2020

i was trying to solve prime generator problem and below is my code.
i tried several test cases but didnt get any wrong output. please tell where am i getting wrong

import java.io.*;

class A{
boolean checkForPrime(int n){
if( n == 0 || n == 1){
return false;
}
else if( n == 2){
return true;
}
else if( n == 3){
return true;
}
else {
int sqrt = (int)Math.sqrt(n);

		for(int i = 2;i<= sqrt;i++){
			if( n % i != 0){
				continue;
			}else{
				return false;
			}
		}
		return true;
	}
}

}

class Main{
public static void main(String args[])throws Exception{

	A ab = new A();
	int a,b , input;
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	String e =br.readLine();
		try{
			input = Integer.parseInt(e);
			for(int i=0;i<input;i++){
				try{
						String c = br.readLine();
						String d =br.readLine();
						a = Integer.parseInt(c);
						b = Integer.parseInt(d);
						for(int j = a; j<= b ;j++){
							if(ab.checkForPrime(j)){
							System.out.println(j);
								}else{
									continue;
								}
						}
				
				}
				catch (NumberFormatException nfe) {
			
				}
			}
		}catch (NumberFormatException nfe) {
			
		}		
}

}

  • created

    Feb '20
  • last reply

    Feb '20
  • 2

    replies

  • 579

    views

  • 2

    users

  • 1

    link

You do realise both numbers are given on the same line? Check the input format.

Suggested Topics

Topic Category Replies Views Activity
Online Judge System 1 120 Mar 26

Want to read more? Browse other topics in Online Judge System or view latest topics.