Count number of occurences of a digit in the range Left limit, right limit and the digit to be counted is entered. This code works only for whole numbers i. e. from 0 to n. This code returns the number of occurence of the digit n in the given range. Please review this code and suggest improvements.

#include &lt;iostream&gt;

int count_n(int l, int r, int n)
{
    if (r &lt;= l)
    {
        std::cerr &lt;&lt; "Right limit must greater then left limit\n";
        return 0;
    }

    if (l &lt; 0)
    {
        std::cerr &lt;&lt; "Only positive integers\n";
        return 0;
    }

    int count = 0;
    for (int i = l; i &lt;= r; ++i)
    {
        int i_copy = i;
        while (i_copy != 0)
        {
            if (i_copy % 10  == n)
            {
                count++;
            }
            i_copy = i_copy / 10;
        }
    }
    return count;
}

int main()
{
    int left_limit, right_limit, num;
    std::cout &lt;&lt; "Enter left limit, right limit and the number(Only positive numbers)\n";
    std::cin &gt;&gt; left_limit &gt;&gt; right_limit &gt;&gt; num;

    int result = count_n(left_limit, right_limit, num);
    std::cout &lt;&lt; result &lt;&lt; "\n";
}


Output:

Enter left limit, right limit and the number(Only for positive numbers)
1 20 0
2
In addition to this, you should specify validation for n ( digit ).
Also, it may be better to group validations into separate function.

void check_input(int min_left, int max_right, int digit) {
  if (max_right &lt;= min_left)
    throw std::invalid_argument("right is &lt;= left, expected right &gt; left");
  if (min_left &lt; 0)
    throw std::invalid_argument("left &lt; 0, expected left &gt; 0");
  if (digit &lt;= 0)
    throw std::invalid_argument("digit &lt;= 0");
    //throw std::invalid_argument("number &lt;= 0");  
  if (digit &gt; 9)
    throw std::invalid_argument("digit &gt; 9");

  ////number:
  //if (digit &gt; max_right)
  //  throw std::invalid_argument("number &gt; right limit");
}

int count_digit_entries(int min_left, int max_right, int digit) {
//...
}

int main() {
  int left_limit, right_limit, num;
  std::cout &lt;&lt; "Enter left limit, right limit and the number(Only positive numbers)\n";
  std::cin &gt;&gt; left_limit &gt;&gt; right_limit &gt;&gt; num;

  check_input(left_limit, right_limit, num);

  int result = count_digit_entries(left_limit, right_limit, num);

  std::cout &lt;&lt; "\nTotal: " &lt;&lt; result &lt;&lt; "\n";
}


You may want to find not only digits but a whole numbers.

//...


#define DEBUG
#ifdef  DEBUG
#define debug(x) x
#else 
#define debug(x)
#endif


int count_digit_entries(int min_left, int max_right, int search_number) {
  int count = 0;

  int mod = 10;
  while(search_number / mod &gt; 0) mod = mod * 10; // 279 -&gt; 1000

  for (int current_number = min_left, right = max_right; current_number &lt; right; ++current_number) {
    int copied_current_number = current_number; // 542793

    while (copied_current_number &gt;= search_number) {
        // 542793 - 279 % 1000    &gt;0 
        // 54279 - 279 % 1000     =0

      if ( ( (copied_current_number - search_number) % mod ) == 0 ) { 
        debug(std::cout &lt;&lt; current_number &lt;&lt; " ");
        ++count;
      };
      copied_current_number = copied_current_number / 10; 
      // 542793 -&gt; 54279

    };
  };

  return count;
};


Output:

Enter left limit, right limit and the number(Only positive numbers)
0 1000 25
25 125 225 250 251 252 253 254 255 256 257 258 259 325 425 525 625 725 825 925 
Total: 20