# Thread: Is it prime? Logic error

1. ## Is it prime? Logic error

I've got a logic error in here and I am having trouble figuring it out.

Can someone please point out the obvious as I cannot see it.

Code:
```#include <iostream>
#include <math.h>
using namespace std;

int main(){

int i, n, squirt;
bool is_prime = true;

cout << "Enter a number: ";
cin >> n;

squirt = sqrt(static_cast<double>(n));

for(i = 2; i <= squirt; i++){
if(i % squirt == 0){
is_prime = false;
break;
}
}

cout << n << " is ";
if(!is_prime)
cout << "not ";
cout << "a prime number.";

cout << endl;

return 0;
}```
TIA!

2. Look carefully at line 16. It's completely out-to-lunch.

3. Originally Posted by oogabooga
Look carefully at line 16.
Thanks!

Originally Posted by oogabooga
It's completely out-to-lunch.
Not sure what this means.

Referring to previously working code, I fixed it with this:

Code:
```#include <iostream>
#include <math.h>
using namespace std;

int main(){

int i, n, squirt;
bool is_prime = true;

cout << "Enter a number: ";
cin >> n;

squirt = sqrt(static_cast<double>(n));

for(i = 2; i <= squirt; i++){
if(n % squirt == 0){
is_prime = false;
break;
}
}

cout << n << " is ";
if(!is_prime)
cout << "not ";
cout << "a prime number.";

cout << endl;

return 0;
}```

4. Line 16 is still wrong.

out to lunch, Slang . not paying attention or tending to business; negligent: You must have been out to lunch when you wrote that weird report.

5. Originally Posted by oogabooga
Line 16 is still wrong.
Hard to break it, but I did.

This doesn't seem to break:

Code:
```#include <iostream>
#include <math.h>
using namespace std;

int main(){

int i, n, squirt;
bool is_prime = true;

cout << "Enter a number: ";
cin >> n;

squirt = sqrt(static_cast<double>(n));

for(i = 2; i <= squirt; i++){
if(n % i == 0){
is_prime = false;
break;
}
}

cout << n << " is ";
if(!is_prime)
cout << "not ";
cout << "a prime number.";

cout << endl;

return 0;
}```