Check if a number is Full Prime in C++


Here we will see, how to check, whether a number is full prime or not. A number is said to be a full prime, if it is prime, and all of its digits are also prime. Suppose a number is 37, this is full prime. But 97 is not full prime as 9 is not a prime number.

One efficient approach is that; first we have to check whether any digit is present that is not prime. Digits must be in 0 to 9. In that range 2, 3, 5 and 7 are prime, others are not prime. If all are primes, then check whether the number is prime or not.

Example

 Live Demo

#include <iostream>
using namespace std;
bool isPrime(int n){
   for(int i = 2; i<= n/2; i++){
      if(n % i == 0){
         return false;
      }
   }
   return true;
}
bool isDigitPrime(int n) {
   int temp = n, digit;
   while(temp){
      digit = temp % 10;
      if(digit != 2 && digit != 3 && digit != 5 && digit != 7){
         return false;
      }
      temp = temp / 10;
   }
   return true;
}
bool isFullPrime(int n){
   return (isDigitPrime(n) && isPrime(n));
}
int main() {
   int num = 37;
   if(isFullPrime(num)){
      cout << "The number is Full Prime";
   } else {
      cout << "The number is not Full Prime";
   }
}

Output

The number is Full Prime

Updated on: 27-Sep-2019

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements