Find the frequency of a digit in a number
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++
// 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 number 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 Code int main() { // input number N int N = 1122322; // input digit D int D = 2; cout<<frequencyDigits(N,D); return 0; } |
chevron_right
filter_none
Java
// Java program to find // the frequency of a // digit in a number class GFG { // function to find frequency // of digit in a number static 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 Code public 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 |
chevron_right
filter_none
Python3
# Python3 program to find the # frequency of a digit in a number # function to find frequency # of digit in a number def 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 N N = 1122322; # input digit D D = 2; print(frequencyDigits(N, D)); # This code is contributed by mits |
chevron_right
filter_none
C#
// C# program to find the frequency // of a digit in a number using System; class GFG { // function to find frequency // of digit in a number static 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 Code static 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 |
chevron_right
filter_none
PHP
<?php // PHP program to find the frequency // of a digit in a number // function to find frequency // of digit in a number function 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 ?> |
chevron_right
filter_none
Output :
4
Recommended Posts:
- 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
- Perform n steps to convert every digit of a number in the format [count][digit]
- Find frequency of smallest value in an array
- Find the element having different frequency than other array elements
- Check if the frequency of all the digits in a number is same
- Count the number of occurrences of a particular digit in a number
- Special two digit number
- Program to delete Nth digit of a Number
- Check if the first and last digit of number N is prime and their sum is less than K
- Largest and smallest digit of a number
- Largest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X
- C++ Program for Largest K digit number divisible by X
- Print multiples of Unit Digit of Given Number
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.



