Additive Prime Number
Given a number N, the task is to check if N is an Additive Prime Number or not. If N is an Additive Prime Number then print “Yes” else print “No”.
An Additive Prime Number is a prime number P such that the sum of digits of P is also a prime number.
For example, 23 is an Additive prime because 2 + 3 = 5 which is a prime number.
Examples:
Input: N = 23
Output: Yes
Explanation:
Sum of digits of 23 = 2 + 3 = 5.Input: N = 10
Output: No
Approach: The idea is to find the sum of digits of the number N and check if it is prime or not. If sum is a prime number then print “Yes” else print “No”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Check if N is prime or notbool isPrime(int n){ // Corner Cases if (n <= 1) return false; if (n <= 3) return true; // This is checked to skip // middle five numbers if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true;}// Function to get sum of digitsint getSum(int n){ int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } // Return the sum of digits return sum;}// Function to check whether// the given number is// Additive Prime number or notbool isAdditivePrime(int n){ // If number is not prime if (!isPrime(n)) return false; // Check if sum of digits // is prime or not return isPrime(getSum(n));}// Driver Codeint main(){ // Given Number N int N = 23; // Function Call if (isAdditivePrime(N)) cout << "Yes"; else cout << "No";} |
Java
// Java program for the above approachclass GFG{// Check if N is prime or notstatic boolean isPrime(int n){ // Corner Cases if (n <= 1) return false; if (n <= 3) return true; // This is checked to skip // middle five numbers if (n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true;}// Function to get sum of digitsstatic int getSum(int n){ int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } // Return the sum of digits return sum;}// Function to check whether// the given number is// Additive Prime number or notstatic boolean isAdditivePrime(int n){ // If number is not prime if (!isPrime(n)) return false; // Check if sum of digits // is prime or not return isPrime(getSum(n));}// Driver codepublic static void main(String[] args){ // Given Number N int n = 23; // Function Call if (isAdditivePrime(n)) System.out.println("Yes"); else System.out.println("No");}}// This code is contributed by Pratima Pandey |
Python3
# Python3 program for the above approach# Check if N is prime or notdef isPrime(n): # Corner Cases if (n <= 1): return False if (n <= 3): return True # This is checked to skip # middle five numbers if (n % 2 == 0 or n % 3 == 0): return False i = 5 while(i * i <= n): if (n % i == 0 or n % (i + 2) == 0): return False i = i + 6 return True# Function to get sum of digitsdef getSum(n): sum = 0 while (n != 0): sum = sum + n % 10 n = n / 10 # Return the sum of digits return sum# Function to check whether# the given number is# Additive Prime number or notdef isAdditivePrime(n): # If number is not prime if (not isPrime(n)): return False # Check if sum of digits # is prime or not return isPrime(getSum(n))# Driver Code# Given Number NN = 23# Function Callif (isAdditivePrime(N)): print ("Yes")else: print ("No")# This code is contributed by Pratik Basu |
C#
// C# program for the above approachusing System;class GFG{// Check if N is prime or notstatic bool isPrime(int n){ // Corner Cases if (n <= 1) return false; if (n <= 3) return true; // This is checked to skip // middle five numbers if (n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true;}// Function to get sum of digitsstatic int getSum(int n){ int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } // Return the sum of digits return sum;}// Function to check whether// the given number is// Additive Prime number or notstatic bool isAdditivePrime(int n){ // If number is not prime if (!isPrime(n)) return false; // Check if sum of digits // is prime or not return isPrime(getSum(n));}// Driver codepublic static void Main(){ // Given Number N int n = 23; // Function Call if (isAdditivePrime(n)) Console.Write("Yes"); else Console.Write("No");}}// This code is contributed by Code_Mech |
Javascript
<script>// JavaScript program for the above approach// Check if N is prime or notfunction isPrime(n){ // Corner Cases if (n <= 1) return false; if (n <= 3) return true; // This is checked to skip // middle five numbers if (n % 2 == 0 || n % 3 == 0) return false; for(let i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true;}// Function to get sum of digitsfunction getSum(n){ let sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } // Return the sum of digits return sum;}// Function to check whether// the given number is// Additive Prime number or notfunction isAdditivePrime(n){ // If number is not prime if (!isPrime(n)) return false; // Check if sum of digits // is prime or not return isPrime(getSum(n));}// Driver Code// Given Number Nlet n = 23;// Function Callif (isAdditivePrime(n)) document.write("Yes");else document.write("No"); // This code is contributed by susmitakundugoaldanga</script> |
Yes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


