Problem in answer of challenge for Checking Prime Number


What is wrong in my code

hello @shashwat6479

u code will run only for 2.
the idea is to keep a boolean variable .if u find any number in range [2…n-1] which divides n then make this boolean variable false.

now once u r done with iteration
if that boolean variable is true that means u havent find any number that divides n then in that case print prime
otherwise print not prime

check ur updated code->

#include<iostream>
using namespace std;

int main(){
int i,n;
cin>>n;
bool isPrime=true;
for(i=2;i<n;i++){

   if(n%i==0){
      isPrime=false;
      break;
   }
   
}
if(isPrime)
  cout<<"Prime";
  else
  cout<<"Not Prime";

return 0;
}
1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.