# Next smallest prime palindrome in C++

We are given a number N. We need to find the prime palindrome that is greater than N. Let's see an example.

Input

N = 10

Output

11

## Algorithm

• Initialise the number N.

• Write a function to check whether the given number is prime or not.

• Write a function to check whether the given number is palindrome.

• Write a loop that iterates from N + 1 till you find the next prime palindrome.

• Check if the number is prime and palindrome.
• If the number is prime and palindrome.
• Return the number.

## Implementation

Following is the implementation of the above algorithm in C++

#include<bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
bool isPalindrome(int n) {
int num, digit, rev = 0;
n = num;
while (num) {
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
}
return n == rev ? true : false;
}
int getNextSmallestPrimePalindrome(int n) {
int i = n + 1;
while (true) {
if (isPrime(i) && isPalindrome(i)) {
return i;
}
i += 1;
}
}
int main() {
int N = 15;
cout << getNextSmallestPrimePalindrome(N) << endl;
return 0;
}

## Output

If you run the above code, then you will get the following result.

17