fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int getOccurrence(int n, int d)
  5. {
  6. int result = 0; // Initialize result
  7.  
  8. // Count appearances in numbers starting
  9. // from d.
  10. int itr = d;
  11. while (itr <= n)
  12. {
  13. // When the last digit is equal to d
  14. if (itr%10 == d)
  15. result++;
  16.  
  17. // When the first digit is equal to d then
  18. if (itr != 0 && itr/10 == d)
  19. {
  20. // increment result as well as number
  21. result++;
  22. itr++;
  23. }
  24.  
  25. // In case of reverse of number such as 12 and 21
  26. else if (itr/10 == d-1)
  27. itr = itr + (10 - d);
  28. else
  29. itr = itr+10;
  30. }
  31. return result;
  32. }
  33.  
  34. // Driver code
  35. int main(void)
  36. {
  37. int n = 2, d = 30;
  38. cout << getOccurrence(n, d);
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5440KB
stdin
Standard input is empty
stdout
Standard output is empty