Count the number of occurrences of a particular digit in a number | C ++ program to count the number of occurrences of a particular digit in a number ; Function to count the occurrences of the digit D in N ; Loop to find the digits of N ; check if the digit is D ; return the count of the occurrences of D in N ; Driver code
#include <iostream> NEW_LINE using namespace std ; long long int countOccurrances ( long long int n , int d ) { long long int count = 0 ; while ( n > 0 ) { count = ( n % 10 == d ) ? count + 1 : count ; n = n / 10 ; } return count ; } int main ( ) { int d = 2 ; long long int n = 214215421 ; cout << countOccurrances ( n , d ) << endl ; return 0 ; }