Count the occurrence of digit K in a given number N using Recursion | C ++ implementation to count the occurrence of a digit in number using Recursion ; Function to count the digit K in the given number N ; Extracting least significant digit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countdigits ( int n , int k ) { if ( n == 0 ) return 0 ; int digit = n % 10 ; if ( digit == k ) return 1 + countdigits ( n / 10 , k ) ; return countdigits ( n / 10 , k ) ; } int main ( ) { int n = 1000 ; int k = 0 ; cout << countdigits ( n , k ) << endl ; return 0 ; }