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

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ implementation to count  ` `// the occurrence of a digit in  ` `// number using Recursion ` ` `  `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to count the digit K ` `// in the given number N ` `int` `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 ` `int` `main() ` `{ ` `    ``int` `n = 1000; ` `    ``int` `k = 0; ` ` `  `    ``cout << countdigits(n, k) << endl; ` `    ``return` `0; ` `} `

## Java

 `// 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)); ` `    ``} ` `} `

## Python3

 `# Python implementation to count  ` `# the occurrence of a digit in  ` `# number using Recursion ` ` `  `# Function to count the digit K ` `# in the given number N ` `def` `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 Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``n ``=` `1000``; ` `    ``k ``=` `0``; ` `    ``print``(countdigits(n, k)) `

## C#

 `// 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)); ` `    ``} ` `} `

Output:

```3
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : Akanksha_Rai

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.