- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- Find next palindrome prime in C++
- Prime Palindrome in C++
- K-th Smallest Prime Fraction in C++
- Java program to check for prime and find next Prime in Java
- Java methods to check for prime and find the next prime
- What is the HCF of smallest prime number and the smallest composite number?
- Palindrome Partitioning in C++
- Palindrome Integer in C++
- Shortest Palindrome in C++
- Longest Palindrome in C++
- Finding next prime number to a given number using JavaScript
- Smallest prime number just greater than the specified number in JavaScript
- Find the difference between the smallest 3 digit prime number and largest 1digit prime number
- Break a Palindrome in C++
- Palindrome Permutation II in C++

Advertisements