Count the number of occurrences of a particular digit in a number - GeeksforGeeks

05 May, 2021
Given a number N and a digit D, the task is to count the occurrences of D in N.Examples:  
Input: N = 25, D = 2
Output: 1

Input: N = 100, D = 0
Output: 2
 
Approach: 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++
Java
Python3
C#
Javascript
// 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 Nlong 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 codeint main(){     int d = 2;    long long int n = 214215421;     cout << countOccurrances(n, d)         << endl;     return 0;}
// Java program to count the number of occurrences// of a particular digit in a numberimport java.util.*; class GFG{ // Function to count the occurrences// of the digit D in Nstatic 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 codepublic static void main(String args[]){    int d = 2;    int n = 214215421;     System.out.println(countOccurrances(n, d));}} // This code is contributed by Surendra_Gangwar
# 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 Ndef 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 coded = 2n = 214215421print(countOccurrances(n, d)) # This code is contributed by Mohit Kumar
// C# program to count the number// of occurrences of a particular// digit in a numberusing System;class GFG{ // Function to count the occurrences// of the digit D in Nstatic 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 codepublic static void Main(){    int d = 2;    int n = 214215421;     Console.WriteLine(countOccurrances(n, d));}} // This code is contributed by Code_Mech.
<script> // Javascript program to count// the number of occurrences// of a particular digit in a number // Function to count the occurrences// of the digit D in Nfunction countOccurrances(n, d){    let 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 = parseInt(n / 10);    }     // return the count of the    // occurrences of D in N    return count;} // Driver code     let d = 2;    let n = 214215421;     document.write(countOccurrances(n, d)); </script>
3
 
mohit kumar 29
SURENDRA_GANGWAR
Code_Mech
ManasChhabra2
rishavmahato348
number-digits
school-programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to find sum of elements in a given array
Modulo Operator (%) in C/C++ with Examples
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Merge two sorted arrays
Euclidean algorithms (Basic and Extended)
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value