# Prime Palindrome Wa

WA in problem Prime Palindrome
My code -

``````//Prime Palindrome
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

bool isPrime(int n)
{
int m = sqrt(n);
bool check = true;
if(n % 2 == 0)
check = false;
else
{
for(int i = 3 ; i <= m ;i += 2)
{
if(n%i == 0)
check = false;
}
if(n%m == 0)
check = false;
}
if(check == false)
return false;
else
return true;
}

bool isPal(int n)
{
int p, q = 0;
p = n;
while(p>0)
{
q=q*10+p%10;
p=p/10;
}
if(q==n)
return true;
else
return false;
}

int main()
{

int n;
cin >> n;

if(n%2 == 0)
n++;

while(isPrime(n) == true && isPal(n) == true)
n += 2;

cout << n << endl;

}
``````

You are incrementing ‘n’ when the number is prime and palindrome, but the question asks you to stop there. Your code doesn’t work for the given test case also. It gives 31 as output while the output should be 101.

When I change it while(isPrime(n) == false && isPal(n) == false) it still doesnt give the correct answer

Instead of ‘and’ use ‘or’ operator. ‘&&’ doesn’t work because the loop will terminate when you encounter either a prime number or a palindrome and it need not encounter both. http://en.wikipedia.org/wiki/Logical_conjunction .
Check how ‘and’ and ‘or’ operator works. I think you may understand why it is not working after reading that. If you still don’t get, you can ask again.

Thanks man got it!
Now TLE which I’ll fix.

:-). Happy Coding