For Loop- primeCheck | 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:
n = 1
Output:
No

Example 2:
Input:
n = 2
Output:
Yes
User Task: 
Your task is to complete the provided function isPrime() which should return a string "Yes" if n is prime and "No" if not.
Constraints:
1 ≤ n ≤ 1000
Video:
YouTubeGeeksforGeeks500K subscribersC++ Programming Language Tutorial | Loops in C++ - Part 1 (Entry Controlled Loop) | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.Full screen is unavailable. Learn MoreMore videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:09•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=MbEVFhucVa0" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
0
vikaschandra8051101 week ago
//perfect soln
string isPrime(int n) {   if(n==1){       return "No";   }   if(n==2 || n==3){       return "Yes";   }      for (int i = 2; i <= sqrt(n); i++) {       // Write your logic here              if(n%i ==0){           return "No";       }       // Return "Yes" if n is prime, else return "No"       /*n is prime only if it is not divisible by any i.       We will not reach till n and 1 is already discarded*/   }   return "Yes";} 
+1
anshumanatrey3 weeks ago
C++ ANSWER :
string isPrime(int n) {
    int c=0;
    for (int i = 2; i <= n; i++) {
        if(n%i==0){
            c++;
        }
    }
    if(c>1 || n==1){
        cout<<"No";
    }
    else {
        cout<<"Yes";
    }
}
0
anshumanatrey
This comment was deleted.
0
imluckybanthia4 weeks ago
string isPrime(int n) {   int c=0;   for (int i = 2; i <= n; i++) {             if(n%i==0){          ++c;      }   }   if(c>=2 || n==0||n==1){       cout<<"No";}       else{           cout<<"Yes";       }       
0
sumitd12991 month ago
string isPrime(int n) {   if(n==1){       return "No";   }   for (int i = 2; i <= sqrt(n); i++) {       if(n%i==0){           return "No";       }   }   return "Yes";}
0
akash119027401 month ago
string isPrime(int n) {   if(n==1)return "No";   if(n==2 || n==3) return "Yes";   if(n%2==0 || n%3==0) return "No";   for(int i = 5; i*i<=n;i+=6){       if(n%i==0 || n%(i+2)==0) return "No";   }   return "Yes";}
0
srilakshmimounika01 month ago
#include<bits/stdc++.h>
using namespace std;
int main()
string isprime(int n)
{
for(int i=2;i,=sqrt(n);i++)
{
if(n%i==0)
return “no”;
}
if(n==1)
return “no”;
else 
return “yes”;
}
return 0;
}
+1
saikumar0272071 month ago
1
0
mdshahid132 months ago
string isPrime(int n) {   for (int i = 2; i <= sqrt(n); i++) {       if(n%i==0)       return "No";   }   if(n==1)   return "No";   else    return "Yes";}
0
nagaajayk2 months ago
if(n==1) {
        return "No";
    }
    for (int i = 2; i <= sqrt(n); i++) {
        if(n%i==0) {
            return "No";
        }
    }
    return "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.