What is wrong in my code ? please help

import java.util.Scanner;
import java.lang.;
import java.io.
;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
int k=0;
for(int i=0;i<t;i++)
{
int n = sc.nextInt();
if(n==1)
System.out.println(“NO”);
for(int j=2; j<n/2 ;j++)
{
if( n%j == 0)
{
k++;
break;
}

	  }
	  if(k==0)
	    System.out.println("YES");
	   else
	    System.out.println("NO");
	}
	}
	catch(Exception e){
	    return;
	}
}

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also - which Problem are you trying to solve? :slight_smile:

problem : PRB01 Problem - CodeChef
my submission : CodeChef: Practical coding for everyone

1 Like
  • The output should be yes or no, not YES or NO :slight_smile:
  • For the other problem - here’s the simplest test input I can think of:
2
6
2

Hopefully you can figure it out from there :slight_smile:

No, still it is giving wrong answer .

Have you fixed your solution so that it gives the correct answer for:

2
6
2

?

It is giving wrong o/p

https://www.codechef.com/viewsolution/47859590
here is my code after modifications

1 Like
1
1

The output should be “yes” or “no”. Here is the logic for a prime number:

public static boolean isPrime(int n)
{
	    boolean prime = true;
	    for (int i = 2; i <= Math.sqrt(n); i++)
	    {
	        if (n % i == 0)
	        {
	            prime = false;
	            break;
	        }
	    }
	    
	    return prime;
}

https://www.codechef.com/viewsolution/32430242