Find the frequency of a digit in a number - GeeksforGeeks

16 Apr, 2021
Given a number N and a digit D. Write a program to find how many times the digit D appears in the number N.Examples :  
Input: N = 1122322  ,  D = 2
Output: 4

Input: N = 346488  ,  D = 9
Output: 0
 
The idea to solve this problem is to keep extracting digits from the number N and check the extracted digits with the given digit D. If the extracted digit is equals to the digit D then increment the count. Below is the implementation of above approach.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the frequency// of a digit in a number#include <bits/stdc++.h>using namespace std; // function to find frequency of digit// in a numberint frequencyDigits(int n, int d){      // Counter variable to store    // the frequency    int c = 0;         // iterate till number reduces to zero    while (n > 0) {                 // check for equality        if (n % 10 == d)            c++;        // reduce the number        n = n / 10;    }         return c;} // Driver Codeint main(){         // input number N    int N = 1122322;         // input digit D    int D = 2;         cout<<frequencyDigits(N,D);     return 0;}
// Java program to find// the frequency of a// digit in a numberclass GFG{ // function to find frequency// of digit in a numberstatic int frequencyDigits(int n,                           int d){    // Counter variable to    // store the frequency    int c = 0;         // iterate till number    // reduces to zero    while (n > 0)    {                 // check for equality        if (n % 10 == d)            c++;        // reduce the number        n = n / 10;    }    return c;} // Driver Codepublic static void main(String args[]){         // input number N    int N = 1122322;         // input digit D    int D = 2;         System.out.println(frequencyDigits(N, D));}} // This code is contributed by Arnab Kundu
# Python3 program to find the# frequency of a digit in a number # function to find frequency# of digit in a numberdef frequencyDigits(n, d):         # Counter variable to    # store the frequency    c = 0;         # iterate till number    # reduces to zero    while (n > 0):                 # check for equality        if (n % 10 == d):            c += 1;        # reduce the number        n = int(n / 10);     return c; # Driver Code # input number NN = 1122322; # input digit DD = 2; print(frequencyDigits(N, D)); # This code is contributed by mits
// C# program to find the frequency// of a digit in a numberusing System; class GFG{ // function to find frequency// of digit in a numberstatic int frequencyDigits(int n,                           int d){    // Counter variable to    // store the frequency    int c = 0;         // iterate till number    // reduces to zero    while (n > 0)    {                 // check for equality        if (n % 10 == d)            c++;        // reduce the number        n = n / 10;    }    return c;} // Driver Codestatic public void Main(String []args){         // input number N    int N = 1122322;         // input digit D    int D = 2;         Console.WriteLine(frequencyDigits(N, D)); }} // This code is contributed by Arnab Kundu
<?php// PHP program to find the frequency// of a digit in a number // function to find frequency// of digit  in a numberfunction frequencyDigits($n, $d){    // Counter variable to    // store the frequency    $c = 0;         // iterate till number    // reduces to zero    while ($n > 0)    {                 // check for equality        if ($n % 10 == $d)            $c++;        // reduce the number        $n = $n / 10;    }         return $c;} // Driver Code // input number N$N = 1122322; // input digit D$D = 2; echo frequencyDigits($N, $D); // This code is contributed by mits?>
<script> // Javascript program to find// the frequency of a// digit in a number // Function to find frequency// of digit in a numberfunction frequencyDigits(n, d){         // Counter variable to    // store the frequency    var c = 0;         // Iterate till number    // reduces to zero    while (n > 0)    {                 // Check for equality        if (n % 10 == d)            c++;                     // Reduce the number        n = parseInt(n / 10);    }    return c;} // Driver code // input number Nvar N = 1122322; // input digit Dvar D = 2; document.write(frequencyDigits(N, D)); // This code is contributed by Khushboogoyal499 </script>
4
 
andrew1234
Mithun Kumar
khushboogoyal499
school-programming
C++ Programs
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Program for QuickSort
Shallow Copy and Deep Copy in C++
delete keyword in C++
cin in C++
Passing a function as a parameter in C++
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects