Given two integers **L **and **R** denoting the starting and end values of a range, the task is to count all numbers in that range whose all digit are same, like 1, 22, 444, 3333, etc.

**Example:**

Input:L = 12, R = 68

Output:5

Explanation:

{ 22, 33, 44, 55, 66} are the numbers with same digits in the given range.

Input:L = 1, R = 32

Output:11

**Naive Approach:** Iterate through all the numbers from L to R and for each number, check if it has all its digits same. If yes, then increase the required count. Print this count at the end.

**Efficient Approach:** The idea is based on the fact that the multiples (1 to 9) of 1, 11, 111, etc has all its digits same.

For example:

1 times 1 = 1 (All digits are same) 2 times 1 = 2 (All digits are same) 3 times 1 = 3 (All digits are same) . . 9 times 1 = 9 (All digits are same) Similarly 1 times 11 = 11 (All digits are same) 2 times 11 = 22 (All digits are same) 3 times 11 = 33 (All digits are same) . . 9 times 11 = 99 (All digits are same) Same is the case for 111, 1111, etc.

Therefore, the steps can be defined as:

- Find the number of digits in R. This will decide the length of consecutive 1s to be created, till which we have to check.

For example, if R = 100, then length(R) = 3. Therefore we need to check only the multiples of 1, 11, and 111. - For each length of consecutive 1s from 1 to length(R):
- Multiply them with all values from
**2**to**9** - Check if it lies within the range
**[L, R]**or not. - If yes, then increment the count of required numbers.

- Multiply them with all values from
- Print the required count of numbers.
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers from range [L, R] whose sum of digits is Y
- Count of numbers from range [L, R] that end with any of the given digits
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count all prime numbers in a given range whose sum of digits is also prime
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Total numbers with no repeated digits in a range
- Cumulative product of digits of all numbers in the given range
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Find all the possible numbers in a range that can be evenly divided by its digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Print all numbers in given range having digits in strictly increasing order
- Count numbers with same first and last digits
- Count Odd and Even numbers in a range from L to R
- Count of numbers having only 1 set bit in the range [0, n]

Below code is the implementation of the above approach:

## C++

`// C++ program to count the ` `// total numbers in the range ` `// L and R which have all the ` `// digit same ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that count the ` `// total numbersProgram between L ` `// and R which have all the ` `// digit same ` `int` `count_same_digit(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `tmp = 0, ans = 0; ` ` ` ` ` `// length of R ` ` ` `int` `n = ` `log10` `(R) + 1; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// tmp has all digits as 1 ` ` ` `tmp = tmp * 10 + 1; ` ` ` ` ` `// For each multiple ` ` ` `// of tmp in range 1 to 9, ` ` ` `// check if it present ` ` ` `// in range [L, R] ` ` ` `for` `(` `int` `j = 1; j <= 9; j++) { ` ` ` ` ` `if` `(L <= (tmp * j) ` ` ` `&& (tmp * j) <= R) { ` ` ` ` ` `// Increment the required count ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `L = 12, R = 68; ` ` ` ` ` `cout << count_same_digit(L, R) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the ` `// total numbers in the range ` `// L and R which have all the ` `// digit same ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function that count the total ` `// numbersProgram between L and ` `// R which have all the digit same ` `static` `int` `count_same_digit(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `tmp = ` `0` `, ans = ` `0` `; ` ` ` ` ` `// Length of R ` ` ` `int` `n = (` `int` `)Math.log10(R) + ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// tmp has all digits as 1 ` ` ` `tmp = tmp * ` `10` `+ ` `1` `; ` ` ` ` ` `// For each multiple of tmp ` ` ` `// in range 1 to 9, check if ` ` ` `// it present in range [L, R] ` ` ` `for` `(` `int` `j = ` `1` `; j <= ` `9` `; j++) ` ` ` `{ ` ` ` `if` `(L <= (tmp * j) && (tmp * j) <= R) ` ` ` `{ ` ` ` `// Increment the required count ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `L = ` `12` `, R = ` `68` `; ` ` ` ` ` `System.out.println(count_same_digit(L, R)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the ` `# total numbers in the range ` `# L and R which have all the ` `# digit same ` `import` `math ` ` ` `# Function that count the ` `# total numbersProgram between L ` `# and R which have all the ` `# digit same ` `def` `count_same_digit(L, R): ` ` ` ` ` `tmp ` `=` `0` `; ans ` `=` `0` `; ` ` ` ` ` `# length of R ` ` ` `n ` `=` `int` `(math.log10(R) ` `+` `1` `); ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` ` ` `# tmp has all digits as 1 ` ` ` `tmp ` `=` `tmp ` `*` `10` `+` `1` `; ` ` ` ` ` `# For each multiple ` ` ` `# of tmp in range 1 to 9, ` ` ` `# check if it present ` ` ` `# in range [L, R] ` ` ` `for` `j ` `in` `range` `(` `1` `, ` `9` `): ` ` ` ` ` `if` `(L <` `=` `(tmp ` `*` `j) ` `and` `(tmp ` `*` `j) <` `=` `R): ` ` ` ` ` `# Increment the required count ` ` ` `ans ` `+` `=` `1` `; ` ` ` ` ` `return` `ans; ` ` ` `# Driver Code ` `L ` `=` `12` `; R ` `=` `68` `; ` ` ` `print` `(count_same_digit(L, R)) ` ` ` `# This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the ` `// total numbers in the range ` `// L and R which have all the ` `// digit same ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that count the total ` `// numbersProgram between L and ` `// R which have all the digit same ` `static` `int` `count_same_digit(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `tmp = 0, ans = 0; ` ` ` ` ` `// Length of R ` ` ` `int` `n = (` `int` `)Math.Log10(R) + 1; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// tmp has all digits as 1 ` ` ` `tmp = tmp * 10 + 1; ` ` ` ` ` `// For each multiple of tmp ` ` ` `// in range 1 to 9, check if ` ` ` `// it present in range [L, R] ` ` ` `for` `(` `int` `j = 1; j <= 9; j++) ` ` ` `{ ` ` ` `if` `(L <= (tmp * j) && (tmp * j) <= R) ` ` ` `{ ` ` ` `// Increment the required count ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `L = 12, R = 68; ` ` ` ` ` `Console.Write(count_same_digit(L, R)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity: **O(length(R))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.