Find next palindrome prime | CPP program to find next palindromic prime for a given number . ; if ( 8 <= N <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= N and it is a prime number then return it . ; Driver code
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool isPrime ( int num ) { if ( num < 2 num % 2 == 0 ) return num == 2 ; for ( int i = 3 ; i * i <= num ; i += 2 ) if ( num % i == 0 ) return false ; return true ; } int primePalindrome ( int N ) { if ( 8 <= N && N <= 11 ) return 11 ; for ( int x = 1 ; x < 100000 ; ++ x ) { string s = to_string ( x ) , r ( s . rbegin ( ) , s . rend ( ) ) ; int y = stoi ( s + r . substr ( 1 ) ) ; if ( y >= N && isPrime ( y ) ) return y ; } return -1 ; } int main ( ) { cout << primePalindrome ( 112 ) ; return 0 ; }