For Loop- primeCheck - Java | Practice | GeeksforGeeks

What do you do when you need to execute certain statements more than once? You put them in a loop. Loops are very powerful. Majority of coding questions need loops to work. You can't even input testcases without loops!
Here, we will use for loop and check if the given number n is prime or not.
Note: A number is prime if it's divisible by itself and 1. Also, 1 is not prime.
Example 1:
Input:
1

Output:
No

Example 2:
Input:
2

Output:
Yes
User Task: 
Your task is to complete the provided function. 
Constraints:
1 <= a <= 1000
0
gyaninfo1281 month ago
class Geeks {
static void isPrime(int n){
if(n==1){
System.out.println("No");
return ;
}
for(int i=2;i≤Math.sqrt(n);i++){
if(n%i==0){
System.out.println("No");
return ;
}}
System.out.println("Yes");
}}
 
+1
badgujarsachin831 month ago
 static void isPrime(int n) {
       int cheak=1;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            // Your code here
            if(n%i==0 && n!=2){
                cheak=0;
            }
        }
        if(n==1){
            cheak=0;
        }
        if(cheak==1){
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
    }
0
sachinmishraravi2 months ago
//java

class Geeks {
    static void isPrime(int n) {
        boolean flag = true;
        if(n == 1 || n==0) {
            System.out.println("No");
            return;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if(n%i == 0 ){ 
                flag = false;
                break;
            }
        }
        
        if(flag)
        System.out.println("Yes");
        else 
        System.out.println("No");
    }
}
+1
piyushchpkumar3 months ago
Simple Java Solution  
 
 static void isPrime(int n) {              int t = 1;       for (int i = 2; i <= Math.sqrt(n)+1; i++) {           if(n%i==0 && n!=2 ) {               t = 0;               break;           }       }              if(n==1) t =0;       if(t==0)System.out.println("No");       if(t==1)System.out.println("Yes");          }
-1
ramprasana4 months ago
When I try Compile & Run I am able to get the response properly, but when I submit I am getting results for each test case run print consecutively, which is caused result failure. If I execute the same input in the compile & run I am getting results correctly.  Please let me know what I am doing wrong 
-1
prajjwalfire4 months ago
 static void isPrime(int n) {
        
        if(n==1)
        {
            System.out.println("No");
            return ;
        }
        
        for (int i = 2; i <= Math.sqrt(n); i++) {

            if(n%i==0)
            {
                System.out.println("No");
                return ;
            }
        }
        
        System.out.println("Yes");

    }
0
swapniltayal4225 months ago
class Geeks {   static void isPrime(int n) {       int i = 0;       for ( i = 2; i < n; i++) {           if (n % i == 0){               break;           }       }if (i == n){           System.out.print("Yes");       }else {           System.out.print("No");       }System.out.println();   }}
0
sdsapkale5 months ago
class Geeks {   static void isPrime(int n) {       int i=0;       for (i = 2; i <= n-1; i++) {           // Your code here           if(n%i==0)           {               break;           }       }       if(i==n){           System.out.print("Yes");       }else{           System.out.print("No");       }
       System.out.println();   }
0
pradeepsgrl125 months ago
class Geeks {
    static void isPrime(int n) {
        boolean flag=false;
        if(n<2)
        {
            System.out.print("No");
            
        }
        else
        {
        for (int i = 2; i <= Math.sqrt(n); i++) {
            // Your code here
            if(n%i==0)
            {
                flag=true;
                break;
            }
        }
        }
        if(flag==true && n>1)
        {
            System.out.print("No");
        }
        else if(flag==false &&n>1)
        {
            System.out.print("Yes");
        }

        System.out.println();
    }
}
0
balahari6195 months ago
class Geeks {   static void isPrime(int n) {       if(n<=1)       System.out.println("No");       else if(n==2 || n==3)       System.out.println("Yes");       else if(n%2==0 || n%3==0)       System.out.println("No");       else {           for (int i = 5; i <= Math.sqrt(n); i++) {               // Your code here               if(n%i==0) {               System.out.println("No");               }           }           System.out.println("Yes");       }   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.