PLZ HAV A LOOK ON CODE AND TELL ME WHY I’M GETTING WRONG ANSWER

``````#include<stdio.h>

int main()
{
int i, j, n, temp, flag, rem, rev;

scanf("%d", &n);
for (i = n + 1; i <= 1004000; i++)
{
temp = i;
rev = 0;
while (temp != 0)
{
rem = temp % 10;
rev = rev * 10 + rem;
temp /= 10;

}
if (rev == i)
{
flag = 0;

for (j = 2; j < i / 2; j++)
if (i%j == 0)
{

flag = 1;

break;
}
if (flag == 0)
{
printf("%d\n", i);
break;
}

}

}
return 0;
}``````

The question asks for Your output must consist of a single integer, the smallest prime
palindrome greater than or equal to N. In your code you are doing : for (i = n + 1; i <= 1004000; i++) which is basically finding the prime palindrome strictly greater than n.

Your loop should be : for (i = n ; i <= 1004000; i++) i.e. start from n instead of n+1.
Changing n to n+1 gets your code AC . Here is the accepted version of your code.

you are using a brute force approach for checking primes and reverse. You should try optimizing that approach. Although it does not matter much in this problem, you might face difficulties later on more demanding problems .I would suggest you read about the Rabin Miller Primality test which is a faster method of checking if a number is prime or not.

Besides you could also try simpler things like skipping all the even numbers since they cannot be prime to optimize your code

1 Like

Thanks alot!!