1 / 2
Nov 2017
import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		try{
		Scanner sc=new Scanner(System.in);
         int t=sc.nextInt();
         int[] a=new int[100];
         int[] c=new int[100];
         int m,n,count,ln=0,k=0;
         if(t>10){
             System.out.println("Invalid Input");
             System.exit(0);
         }
         for(int i=0;i<t;i++){
             m=sc.nextInt(); n=sc.nextInt();
             for(int j=m;j<=n;j++){
               count=0;
                 if(j!=1){
                    if(j==2){
                       a[ln]=j;
                       ln++;
                    }
                    else{
                 for(int l=2;l<=j/2;l++){
                     if(j%l==0){
                        count++;
                     }
                     
                    }
                 if(count==0){
                     a[ln]=j;
                     ln++;
                 }}
             }
                 if(j==n){
                     c[k]=ln;
                     k++;
                 }
             }
         }
         int p=0;
         for(int i=0;i<ln;i++){
             System.out.println(a[i]);
             if(c[p]==i+1){
                 System.out.println("");
                 p++;
             }
         }
		}
		catch(Exception e){
			
		}
	}
}

This is my code and its giving correct output and also running properly in Ideone. but still its showing Wrong answer. Can anyone help me?

  • created

    Nov '17
  • last reply

    Nov '17
  • 1

    reply

  • 904

    views

  • 2

    users

  • 1

    like

You are assuming that it won’t be more prime numbers than 100 in your result what is wrong.

You don’t have to validate the input and, moreover, display any messages like “Invalid Input”. It is only information for you what you can expect.

Also you probably should not use empty exception :slight_smile: