Count of Prime digits in a Number - GeeksforGeeks

24 Nov, 2021
Given an integer N, the task is to count the number of prime digits in N.Examples:  
Input: N = 12 Output: 1 Explanation: Digits of the number – {1, 2} But, only 2 is prime number.Input: N = 1032 Output: 2 Explanation: Digits of the number – {1, 0, 3, 2} 3 and 2 are prime number  
Approach: The idea is to iterate through all the digits of the number and check whether the digit is a prime or not. Since there are only four possible prime numbers in the range [0, 9] and every digit for sure lies in this range, we only need to check the number of digits equal to either of the elements in the set {2, 3, 5, 7}. Below is the implementation of this approach:  
CPP
Java
Python3
C#
Javascript
// C++ program to count the number of// prime digits in a number #include <bits/stdc++.h>using namespace std; // Function to find the count of// prime digits in a numberint countDigit(int n){    int temp = n, count = 0;     // Loop to compute all the digits    // of the number    while (temp != 0) {         // Finding every digit of the        // given number        int d = temp % 10;         temp /= 10;         // Checking if digit is prime or not        // Only 2, 3, 5 and 7 are prime        // one-digit number        if (d == 2 || d == 3            || d == 5 || d == 7)            count++;    }     return count;} // Driver codeint main(){    int n = 1234567890;     cout << countDigit(n) << endl;    return 0;}
// Java program to count the number of// prime digits in a numberclass GFG {         // Function to find the count of    // prime digits in a number    static int countDigit(int n)    {        int temp = n, count = 0;             // Loop to compute all the digits        // of the number        while (temp != 0) {                 // Finding every digit of the            // given number            int d = temp % 10;                 temp /= 10;                 // Checking if digit is prime or not            // Only 2, 3, 5 and 7 are prime            // one-digit number            if (d == 2 || d == 3                || d == 5 || d == 7)                count++;        }             return count;    }         // Driver code    public static void main (String[] args)    {        int n = 1234567890;             System.out.println(countDigit(n)) ;     }} // This code is contributed by AnkitRai01
# Python3 program to count the number of# prime digits in a number # Function to find the count of# prime digits in a numberdef countDigit(n):    temp = n    count = 0     # Loop to compute all the digits    # of the number    while (temp != 0):         # Finding every digit of the        # given number        d = temp % 10         temp //= 10         # Checking if digit is prime or not        # Only 2, 3, 5 and 7 are prime        # one-digit number        if (d == 2 or d == 3 or d == 5 or d == 7):            count += 1     return count # Driver codeif __name__ == '__main__':    n = 1234567890     print(countDigit(n)) # This code is contributed by mohit kumar 29
// C# program to count the number of// prime digits in a numberusing System; class GFG {         // Function to find the count of    // prime digits in a number    static int countDigit(int n)    {        int temp = n, count = 0;             // Loop to compute all the digits        // of the number        while (temp != 0) {                 // Finding every digit of the            // given number            int d = temp % 10;                 temp /= 10;                 // Checking if digit is prime or not            // Only 2, 3, 5 and 7 are prime            // one-digit number            if (d == 2 || d == 3                || d == 5 || d == 7)                count++;        }             return count;    }         // Driver code    public static void Main (string[] args)    {        int n = 1234567890;             Console.WriteLine(countDigit(n)) ;    }} // This code is contributed by AnkitRai01
<script> // JavaScript program to count the number of// prime digits in a number  // Function to find the count of// prime digits in a numberfunction countDigit(n){    let temp = n, count = 0;     // Loop to compute all the digits    // of the number    while (temp != 0) {         // Finding every digit of the        // given number        let d = temp % 10;         temp = Math.floor(temp / 10);         // Checking if digit is prime or not        // Only 2, 3, 5 and 7 are prime        // one-digit number        if (d == 2 || d == 3            || d == 5 || d == 7)            count++;    }     return count;} // Driver code let n = 1234567890; document.write(countDigit(n) + "<br>"); // This code is contributed by gfgking  </script>
4
 
Time Complexity: O(log10N), where N is the length of the number.
Auxiliary Space: O(1) 
mohit kumar 29
ankthon
gfgking
subham348
number-digits
Prime Number
Mathematical
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Program to find sum of elements in a given array
Program for Decimal to Binary Conversion
Program for factorial of a number
Operators in C / C++
The Knight's tour problem | Backtracking-1
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Print all possible combinations of r elements in a given array of size n