# Count numbers with same first and last digits

Given an interval, the task is to count numbers which have same first and last digits. For example, 1231 has same first and last digits.

Examples:

```Input  : start = 7,  end : 68
Output : 9
Number with same first and last digits are,
7 8 9 11 22 33 44 55 66.

Input  : start = 5,  end : 40
Output : 8
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Let us first consider below examples to understand the approach.

```From 120 to 130, only 121 has same starting and ending digit
From 440 to 450, only 444 has same starting and ending digit
From 1050 to 1060, only 1051 has same starting and ending digit
```

From above examples, we can observe that in each ten number span we have only one number which has the given property except 1 to 10 which has 9 numbers (all one digit number) having same starting and ending digit.
Using above property we can solve given problem, first we break the given interval into two parts that is if interval is l to r, we break this into 1 to l and 1 to r, our answer is obtained by subtracting result of first query from second query.
Now we remain to find count of numbers with given property in range 1 to x, for this we divide x by 10, which gives number of 10-spans. We add 9 to the span for taking one digit numbers into account. If last digit of x is smaller than first digit of x, then 1 should be decreased in final answer to discard last ten span number because that number is out of range.

## C++

 `// C++ program to get count of numbers with ` `// same start and end digit in an interval ` `#include ` `using` `namespace` `std; ` ` `  `// Utility method to get first digit of x ` `int` `getFirstDigit(``int` `x) ` `{ ` `    ``while` `(x >= 10) ` `        ``x /= 10; ` `    ``return` `x; ` `} ` ` `  `// method to return count of numbers with same ` `// starting and ending digit from 1 upto x ` `int` `getCountWithSameStartAndEndFrom1(``int` `x) ` `{ ` `    ``if` `(x < 10) ` `        ``return` `x; ` ` `  `    ``// get ten-spans from 1 to x ` `    ``int` `tens = x / 10; ` ` `  `    ``// add 9 to consider all 1 digit numbers ` `    ``int` `res = tens + 9; ` ` `  `    ``// Find first and last digits ` `    ``int` `firstDigit = getFirstDigit(x); ` `    ``int` `lastDigit = x % 10; ` ` `  `    ``// If last digit is greater than first digit ` `    ``// then decrease count by 1 ` `    ``if` `(lastDigit < firstDigit) ` `        ``res--; ` ` `  `    ``return` `res; ` `} ` ` `  `// Method to return count of numbers with same starting ` `// and ending digit between start and end ` `int` `getCountWithSameStartAndEnd(``int` `start, ``int` `end) ` `{ ` `    ``return` `getCountWithSameStartAndEndFrom1(end)  ` `    ``- getCountWithSameStartAndEndFrom1(start - 1); ` `} ` ` `  `// Driver code to test above methods ` `int` `main() ` `{ ` `    ``int` `start = 5, end = 40; ` ` `  `    ``cout << getCountWithSameStartAndEnd(start, end); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to get count of numbers with ` `// same start and end digit in an interval ` `import` `java.util.*; ` ` `  `class` `Digits { ` `    ``// Utility method to get first digit of x ` `    ``public` `static` `int` `getFirstDigit(``int` `x) ` `    ``{ ` `        ``while` `(x >= ``10``) ` `            ``x /= ``10``; ` `        ``return` `x; ` `    ``} ` ` `  `    ``// method to return count of numbers with same ` `    ``// starting and ending digit from 1 upto x ` `    ``public` `static` `int` `getCountWithSameStartAndEndFrom1(``int` `x) ` `    ``{ ` `        ``if` `(x < ``10``) ` `            ``return` `x; ` ` `  `        ``// get ten-spans from 1 to x ` `        ``int` `tens = x / ``10``; ` ` `  `        ``// add 9 to consider all 1 digit numbers ` `        ``int` `res = tens + ``9``; ` ` `  `        ``// Find first and last digits ` `        ``int` `firstDigit = getFirstDigit(x); ` `        ``int` `lastDigit = x % ``10``; ` ` `  `        ``// If last digit is greater than first ` `        ``// digit then decrease count by 1 ` `        ``if` `(lastDigit < firstDigit) ` `            ``res--; ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Method to return count of numbers with same ` `    ``// starting and ending digit between start and end ` `    ``public` `static` `int` `getCountWithSameStartAndEnd(``int` `start, ` `                                                  ``int` `end) ` `    ``{ ` `        ``return` `getCountWithSameStartAndEndFrom1(end)  ` `        ``- getCountWithSameStartAndEndFrom1(start - ``1``); ` `    ``} ` ` `  `    ``// driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `start = ``5``, end = ``40``; ` `        ``System.out.print(getCountWithSameStartAndEnd(start, ` `                                                     ``end)); ` `    ``} ` `} ` ` `  `// This code is contributed by rishabh_jain `

## Python3

 `# Python3 program to get count of numbers with ` `# same start and end digit in an interval ` ` `  `# Utility method to get first digit of x ` `def` `getFirstDigit(x) : ` `    ``while` `(x >``=` `10``) : ` `        ``x ``/``=` `10` `    ``return` `x ` ` `  `# method to return count of numbers with same ` `# starting and ending digit from 1 upto x ` `def` `getCountWithSameStartAndEndFrom1(x) : ` `    ``if` `(x < ``10``): ` `        ``return` `x ` ` `  `    ``# get ten-spans from 1 to x ` `    ``tens ``=` `x ``/` `10` ` `  `    ``# add 9 to consider all 1 digit numbers ` `    ``res ``=` `tens ``+` `9` ` `  `    ``# Find first and last digits ` `    ``firstDigit ``=` `getFirstDigit(x) ` `    ``lastDigit ``=` `x ``%` `10` ` `  `    ``# If last digit is greater than first digit ` `    ``# then decrease count by 1 ` `    ``if` `(lastDigit < firstDigit) : ` `        ``res ``=` `res ``-` `1` ` `  `    ``return` `res ` ` `  `# Method to return count of numbers with same starting ` `# and ending digit between start and end ` `def` `getCountWithSameStartAndEnd(start, end) : ` `    ``return` `(getCountWithSameStartAndEndFrom1(end) ``-`  `           ``getCountWithSameStartAndEndFrom1(start ``-` `1``)) ` ` `  `# Driver Code ` `start ``=` `5` `end ``=` `40` `print``(getCountWithSameStartAndEnd(start, end)) ` ` `  `# This code is contributed by rishabh_jain `

## C#

 `// C# program to get count of numbers with ` `// same start and end digit in an interval ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Utility method to get first digit ` `    ``// of x ` `    ``public` `static` `int` `getFirstDigit(``int` `x) ` `    ``{ ` `        ``while` `(x >= 10) ` `            ``x /= 10; ` `             `  `        ``return` `x; ` `    ``} ` ` `  `    ``// method to return count of numbers  ` `    ``// with same starting and ending digit ` `    ``// from 1 upto x ` `    ``public` `static` `int` `getCountWithSameStartAndEndFrom1( ` `                                                  ``int` `x) ` `    ``{ ` `         `  `        ``if` `(x < 10) ` `            ``return` `x; ` ` `  `        ``// get ten-spans from 1 to x ` `        ``int` `tens = x / 10; ` ` `  `        ``// add 9 to consider all 1 digit ` `        ``// numbers ` `        ``int` `res = tens + 9; ` ` `  `        ``// Find first and last digits ` `        ``int` `firstDigit = getFirstDigit(x); ` `        ``int` `lastDigit = x % 10; ` ` `  `        ``// If last digit is greater than  ` `        ``// first digit then decrease count ` `        ``// by 1 ` `        ``if` `(lastDigit < firstDigit) ` `            ``res--; ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Method to return count of numbers  ` `    ``// with same starting and ending digit ` `    ``// between start and end ` `    ``public` `static` `int` `getCountWithSameStartAndEnd( ` `                                 ``int` `start, ``int` `end) ` `    ``{ ` `        ``return` `getCountWithSameStartAndEndFrom1(end)  ` `        ``- getCountWithSameStartAndEndFrom1(start - 1); ` `    ``} ` ` `  `    ``// driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `start = 5, end = 40; ` `         `  `        ``Console.Write(getCountWithSameStartAndEnd(start, ` `                                                ``end)); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal. `

## PHP

 `= 10) ` `        ``\$x` `/= 10; ` `    ``return` `\$x``; ` `} ` ` `  `// method to return count ` `// of numbers with same ` `// starting and ending  ` `// digit from 1 upto x ` `function` `getCountWithSameStartAndEndFrom1(``\$x``) ` `{ ` `    ``if` `(``\$x` `< 10) ` `        ``return` `\$x``; ` ` `  `    ``// get ten-spans from 1 to x ` `    ``\$tens` `= ``\$x` `/ 10; ` ` `  `    ``// add 9 to consider all ` `    ``// 1 digit numbers ` `    ``\$res` `= ``\$tens` `+ 9; ` ` `  `    ``// Find first and last digits ` `    ``\$firstDigit` `= getFirstDigit(``\$x``); ` `    ``\$lastDigit` `= ``\$x` `% 10; ` ` `  `    ``// If last digit is greater ` `    ``// than first digit ` `    ``// then decrease count by 1 ` `    ``if` `(``\$lastDigit` `< ``\$firstDigit``) ` `        ``\$res``--; ` ` `  `    ``return` `\$res``; ` `} ` ` `  `// Method to return count of  ` `// numbers with same starting ` `// and ending digit between ` `// start and end ` `function` `getCountWithSameStartAndEnd(``\$start``, ``\$end``) ` `{ ` `    ``return` `getCountWithSameStartAndEndFrom1(``\$end``)  ` `          ``- getCountWithSameStartAndEndFrom1(``\$start` `- 1); ` `} ` ` `  `    ``// Driver Code ` `    ``\$start` `= 5; ` `    ``\$end` `= 40; ` `    ``echo` `getCountWithSameStartAndEnd(``\$start``, ``\$end``); ` ` `  `// This code is contributed by nitin mittal.  ` `?> `

Output:

```8
```

This article is contributed by Utkarsh Trivedi. 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.

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.

My Personal Notes arrow_drop_up

Improved By : nitin mittal

Article Tags :
Practice Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.