Count the occurrence of digit K in a given number N using Recursion - GeeksforGeeks

24 Nov, 2021
Given an integer N greater than 0, the task is to find the occurrence of digit K present in the given number N.Examples:  
Input: N = 10000, K = 0 Output: 4 Explanation: Occurrence of ‘0’ digit in 10000 is 4.Input: N = 51435, K = 5 Output: 2 Explanation: Occurrence of ‘5’ digit in 51435 is 2  
 
Approach: The idea is to use recursion to extract the least significant digit of the number in each step and Check that the digit extracted is equal to the given digit K or not. Finally, recurse for checking the next digits of the number by computing the integer division of that number by 10.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to count// the occurrence of a digit in// number using Recursion #include <bits/stdc++.h> using namespace std; // Function to count the digit K// in the given number Nint countdigits(int n, int k){    if (n == 0)        return 0;     // Extracting least    // significant digit    int digit = n % 10;    if (digit == k)        return 1 + countdigits(n / 10, k);     return countdigits(n / 10, k);} // Driver Codeint main(){    int n = 1000;    int k = 0;     cout << countdigits(n, k) << endl;    return 0;}
// Java implementation to count// the occurrence of a digit in// number using Recursion import java.util.*;  class GFG {         // Function to count the digit K    // in the given number N    static double countdigits(int n, int k)    {        if (n == 0)            return 0;          // Extracting least        // significant digit        int digit = n % 10;        if (digit == k)            return 1 + countdigits(n / 10, k);          return countdigits(n / 10, k);    }      // Driver Code    public static void main(String[] args)    {        int n = 1000;        int k = 0;        System.out.println(countdigits(n, k));    }}
# Python implementation to count# the occurrence of a digit in# number using Recursion # Function to count the digit K# in the given number Ndef countdigits(n, k):    if n == 0:        return 0              # Extracting least    # significant digit    digit = n % 10          if digit == k:        return 1 + countdigits(n / 10, k)      return countdigits(n / 10, k) # Driver Codeif __name__ == "__main__":    n = 1000;    k = 0;    print(countdigits(n, k))
// C# implementation to count// the occurrence of a digit in// number using Recursion using System;  class GFG {         // Function to count the digit K    // in the given number N    static double countdigits(int n,                              int k)    {        if (n == 0)            return 0;          // Extracting least        // significant digit        int digit = n % 10;        if (digit == k)            return 1 +             countdigits(n / 10, k);          return countdigits(n / 10, k);    }      // Driver Code    static public void Main()    {        int n = 1000;        int k = 0;        Console.WriteLine(countdigits(n, k));    }}
<script> // Javascript implementation to count// the occurrence of a digit in// number using Recursion  // Function to count the digit K// in the given number Nfunction countdigits(n, k){    if (n == 0)        return 0;     // Extracting least    // significant digit    var digit = n % 10;    if (digit == k)        return 1 + countdigits(n / 10, k);     return countdigits(n / 10, k);} // Driver Codevar n = 1000;var k = 0;document.write( countdigits(n, k));  </script>
3
 
Time Complexity: O(log10n)
Auxiliary Space: O(log10n)
Akanksha_Rai
rutvik_56
subham348
Mathematical
Recursion
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Fizz Buzz Implementation
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Program for Sum of the digits of a given number
Backtracking | Introduction