# Count the number of occurrences of a particular digit in a number

Given a number **N** and a digit **D**, the task is to count the occurrences of **D in N**.

**Examples:**

Input:N = 25, D = 2Output:1Input:N = 100, D = 0Output:2

**Appraoch:** Take out the digits one by one in N and check if this digit is equal to D. If equal, then increment the count by 1. In the end, print the count.

Below is the implementation of the above approach:

## C++

`// C++ program to count the number of occurrences ` `// of a particular digit in a number ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to count the occurrences ` `// of the digit D in N ` `long` `long` `int` `countOccurrances(` `long` `long` `int` `n, ` ` ` `int` `d) ` `{ ` ` ` `long` `long` `int` `count = 0; ` ` ` ` ` `// Loop to find the digits of N ` ` ` `while` `(n > 0) { ` ` ` ` ` `// check if the digit is D ` ` ` `count = (n % 10 == d) ` ` ` `? count + 1 ` ` ` `: count; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `// return the count of the ` ` ` `// occurrences of D in N ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `d = 2; ` ` ` `long` `long` `int` `n = 214215421; ` ` ` ` ` `cout << countOccurrances(n, d) ` ` ` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the number of occurrences ` `// of a particular digit in a number ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count the occurrences ` `// of the digit D in N ` `static` `int` `countOccurrances(` `int` `n, ` ` ` `int` `d) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Loop to find the digits of N ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` ` ` `// check if the digit is D ` ` ` `count = (n % ` `10` `== d) ? ` ` ` `count + ` `1` `: count; ` ` ` `n = n / ` `10` `; ` ` ` `} ` ` ` ` ` `// return the count of the ` ` ` `// occurrences of D in N ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `d = ` `2` `; ` ` ` `int` `n = ` `214215421` `; ` ` ` ` ` `System.out.println(countOccurrances(n, d)); ` `} ` `} ` ` ` `// This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the ` `# number of occurrences of a ` `# particular digit in a number ` ` ` `# Function to count the occurrences ` `# of the digit D in N ` `def` `countOccurrances(n, d): ` ` ` `count ` `=` `0` ` ` ` ` `# Loop to find the digits of N ` ` ` `while` `(n > ` `0` `): ` ` ` ` ` `# check if the digit is D ` ` ` `if` `(n ` `%` `10` `=` `=` `d): ` ` ` `count ` `=` `count ` `+` `1` ` ` ` ` `n ` `=` `n ` `/` `/` `10` ` ` ` ` `# return the count of the ` ` ` `# occurrences of D in N ` ` ` `return` `count ` ` ` `# Driver code ` `d ` `=` `2` `n ` `=` `214215421` `print` `(countOccurrances(n, d)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the number ` `// of occurrences of a particular ` `// digit in a number ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to count the occurrences ` `// of the digit D in N ` `static` `int` `countOccurrances(` `int` `n, ` ` ` `int` `d) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Loop to find the digits of N ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` ` ` `// check if the digit is D ` ` ` `count = (n % 10 == d) ? ` ` ` `count + 1 : count; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `// return the count of the ` ` ` `// occurrences of D in N ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `d = 2; ` ` ` `int` `n = 214215421; ` ` ` ` ` `Console.WriteLine(countOccurrances(n, d)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Number of occurrences of 2 as a digit in numbers from 0 to n
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count n digit numbers divisible by given number
- Count digit groupings of a number with given constraints
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count numbers with difference between number and its digit sum greater than specific value
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Find the remainder when First digit of a number is divided by its Last digit
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Queries on sum of odd number digit sums of all the factors of a number
- Largest number less than N with digit sum greater than the digit sum of N
- Largest number less than N whose each digit is prime number
- Find the occurrences of digit d in the range [0..n]
- Count number of trailing zeros in Binary representation of a number using Bitset

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.