Count the number of occurrences of a particular digit in a number | Java 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
import java . util . * ; class GFG { static int countOccurrances ( int n , int d ) { int count = 0 ; while ( n > 0 ) { count = ( n % 10 == d ) ? count + 1 : count ; n = n / 10 ; } return count ; } public static void main ( String args [ ] ) { int d = 2 ; int n = 214215421 ; System . out . println ( countOccurrances ( n , d ) ) ; } }