Find the occurrences of digit d in the range [0..n]

02 Feb, 2022
Given a number n and a digit d, count all occurrences of d in range from 0 to n.Examples:  
Input :  n = 25 
         d = 2
Output : 9
The occurrences are 2, 12, 20, 21
22 (Two occurrences), 23, 24, 25

Input : n = 25 
        d = 3
Output :3
The occurrences are 3, 13, 23

Input : n = 32 
        d = 3
Output : 6
The occurrences are 3, 13, 23, 30, 31, 32
 
The first occurrence of d cannot be before number d. So we start to iterate from d and do following check again and again. We jump the number by 10 most of the time (in step 2) except for the cases mentioned in steps 2.a and 2.b. Step 1: Check whether the last digit of the number is equal to the d, if it is then increment the count. Step 2: a) If the number is completely divisible by 10 then increment both the count and number (For example if we reach the number 30 which is completely divisible by 10 and d=3, then we have to count all numbers from 31-39 that’s why we increment count by 1 and number by 1) b) else if the first digit of the number is one less than d then it means that we have come to the row where we have to increment the number by 10 and subtract the d from it. For example if we reach 23 for d=3 then we increment the number by 23+10-3 = 30) c) else increment the number by 10 only. (For example: d=3, itr=3, then increment by 10 i.e.13, 23) Step 3:- Return the count. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count appearances of// a digit 'd' in range from [0..n]#include <bits/stdc++.h>using namespace std; int getOccurrence(int n, int d){    int result = 0; // Initialize result     // Count appearances in numbers starting    // from d.    int itr = d;    while (itr <= n)    {        // When the last digit is equal to d        if (itr%10 == d)            result++;         // When the first digit is equal to d then        if (itr != 0 && itr/10 == d)        {            // increment result as well as number            result++;            itr++;        }         // In case of reverse of number such as 12 and 21        else if (itr/10 == d-1)            itr = itr + (10 - d);        else            itr = itr+10;    }    return result;} // Driver codeint main(void){    int n = 11, d = 1;    cout << getOccurrence(n, d);    return 0;}
// java program to count appearances of// a digit 'd' in range from [0..n]import java.*; public class GFG {         static int getOccurrence(int n, int d)    {                 // Initialize result        int result = 0;             // Count appearances in numbers        // starting from d.        int itr = d;                 while (itr <= n)        {                         // When the last digit is            // equal to d            if (itr % 10 == d)                result++;                 // When the first digit is            // equal to d then            if (itr != 0 && itr/10 == d)            {                                 // increment result as                // well as number                result++;                itr++;            }                 // In case of reverse of number            // such as 12 and 21            else if (itr/10 == d-1)                itr = itr + (10 - d);            else                itr = itr + 10;        }                 return result;    }          // Driver code    public static void main (String[] args)    {        int n = 11, d = 1;             System.out.println(getOccurrence(n, d) );    }} // This code is contributed by Sam007.
# Python3 program to count appearances# of a digit 'd' in range from [0..n]import math;def getOccurrence(n, d):         # Initialize result    result = 0;     # Count appearances in numbers    # starting from d.    itr = d;    while(itr <= n):                 # When the last digit is equal to d        if (itr % 10 == d):            result += 1;         # When the first digit is equal to d then        if (itr != 0 and math.floor(itr / 10) == d):                         # increment result as well as number            result += 1;            itr += 1;         # In case of reverse of number        # such as 12 and 21        elif (math.floor(itr / 10) == d - 1):            itr = itr + (10 - d);        else:            itr = itr + 10;     return result; # Driver coden = 11;d = 1;print(getOccurrence(n, d)); # This code is contributed by mits
// C# program to count appearances of// a digit 'd' in range from [0..n]using System;         public class GFG {         static int getOccurrence(int n, int d)    {                 // Initialize result        int result = 0;             // Count appearances in numbers        // starting from d.        int itr = d;        while (itr <= n)        {                         // When the last digit is            // equal to d            if (itr % 10 == d)                result++;                 // When the first digit is            // equal to d then            if (itr != 0 && itr/10 == d)            {                                 // increment result as                // well as number                result++;                itr++;            }                 // In case of reverse of number            // such as 12 and 21            else if (itr/10 == d-1)                itr = itr + (10 - d);            else                itr = itr + 10;        }                 return result;    }         // Driver code    public static void Main()    {        int n = 11, d = 1;                 Console.Write(getOccurrence(n, d));    }} // This code is contributed by Sam007.
<?php// PHP program to count appearances of// a digit 'd' in range from [0..n] function getOccurrence($n, $d){         // Initialize result    $result = 0;     // Count appearances in numbers    // starting from d.    $itr = $d;    while($itr <= $n)    {                 // When the last digit        // is equal to d        if ($itr % 10 == $d)            $result++;         // When the first digit        // is equal to d then        if ($itr != 0 && floor($itr / 10) == $d)        {                         // increment result as            // well as number            $result++;            $itr++;        }         // In case of reverse of        // number such as 12 and 21        else if (floor($itr / 10) == $d - 1)            $itr = $itr + (10 - $d);        else            $itr = $itr + 10;    }    return $result;}     // Driver code    $n = 11;    $d = 1;    echo getOccurrence($n, $d); // This code is contributed by nitin mittal.?>
<script>    // Javascript program to count appearances of// a digit 'd' in range from [0..n] function getOccurrence(n, d){         // Initialize result    let result = 0;     // Count appearances in numbers    // starting from d.    let itr = d;    while(itr <= n)    {                 // When the last digit        // is equal to d        if (itr % 10 == d)            result++;         // When the first digit        // is equal to d then        if (itr != 0 && Math.floor(itr / 10) == d)        {                         // increment result as            // well as number            result++;            itr++;        }         // In case of reverse of        // number such as 12 and 21        else if (Math.floor(itr / 10) == d - 1)            itr = itr + (10 - d);        else            itr = itr + 10;    }    return result;}     // Driver code    let n = 11;    let d = 1;    document.write(getOccurrence(n, d)); // This code is contributed by _saurabh_jaiswal.</script>
Output:  
4
This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
nitin mittal
Mithun Kumar
saikiransripada
_saurabh_jaiswal
sweetyty
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Program for Decimal to Binary Conversion
Modulo 10^9+7 (1000000007)