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

`// 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 number ` `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 ` `int` `main() ` `{ ` ` ` `int` `n = 1234567890; ` ` ` ` ` `cout << countDigit(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the number of ` `// prime digits in a number ` `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` `; ` ` ` ` ` `System.out.println(countDigit(n)) ; ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the number of ` `# prime digits in a number ` ` ` `# Function to find the count of ` `# prime digits in a number ` `def` `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 code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `1234567890` ` ` ` ` `print` `(countDigit(n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the number of ` `// prime digits in a number ` `using` `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 ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** *O(N)*, where N is the length of the number.

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.

## Recommended Posts:

- Count all prime numbers in a given range whose sum of digits is also prime
- Count of prime digits of a Number which divides the number
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Print prime numbers with prime sum of digits in an array
- Sum of prime numbers without odd prime digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Sum of all the prime numbers with the count of digits ≤ D
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.