Given two positive numbers **M** and **N**, the task is to find the count of all numbers having **M** as the last digit from the range **[1, N]**.

**Examples:**

Input:M = 5, N = 15Output:2Explanation:

Only 2 numbers(5 and 15) from the range [1, 15] ends with the digit ‘5’.Input:M = 1, N = 100Output:10

**Naive Approach: **The simplest approach is to iterate over the range **1** to **N** and check if the last digit is equal to **M** or not. If found to be true, then increment the count. Finally, print the count obtained. **Time complexity: **O(N) **Auxiliary Space:** O(1)**Efficient Approach: **To optimize the above approach, the idea is based on the fact that the count of numbers ending with every digit will be the same until the largest multiple of 10 which is less than **N** (say **x**). Therefore, its count will be (N / 10). Now, the task is reduced to compute the count of numbers ending with **M** which are between **x** and **N.**

Below are the steps:

- Initialize a variable to store the total count, say
**total_count**. - Add
**(N / 10)**to the total count. - Compute
**x**to store the largest multiple of 10 which is less than**N**using the formula:

x = (N / 10) * 10

- Now, calculate the count of numbers ending with
**M**lying in between**x**and**N**. - Add this count to the
**total_count**. Print the final value of**total_count**obtained.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the numbers ` `// ending with M ` `int` `getCount(` `int` `N, ` `int` `M) ` `{ ` ` ` ` ` `// Stores the count of ` ` ` `// numbers required ` ` ` `int` `total_count = 0; ` ` ` ` ` `// Calculate count upto ` ` ` `// nearest power of 10 ` ` ` `total_count += (N / 10); ` ` ` ` ` `// Computing the value of x ` ` ` `int` `x = (N / 10) * 10; ` ` ` ` ` `// Adding the count of numbers ` ` ` `// ending at M from x to N ` ` ` `if` `((N - x) >= M) ` ` ` `{ ` ` ` `total_count = total_count + 1; ` ` ` `} ` ` ` `return` `total_count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 100, M = 1; ` ` ` ` ` `// Function Call ` ` ` `cout << getCount(N, M); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count the numbers ` `// ending with M ` `static` `int` `getCount(` `int` `N, ` `int` `M) ` `{ ` ` ` ` ` `// Stores the count of ` ` ` `// numbers required ` ` ` `int` `total_count = ` `0` `; ` ` ` ` ` `// Calculate count upto ` ` ` `// nearest power of 10 ` ` ` `total_count += (N / ` `10` `); ` ` ` ` ` `// Computing the value of x ` ` ` `int` `x = (N / ` `10` `) * ` `10` `; ` ` ` ` ` `// Adding the count of numbers ` ` ` `// ending at M from x to N ` ` ` `if` `((N - x) >= M) ` ` ` `{ ` ` ` `total_count = total_count + ` `1` `; ` ` ` `} ` ` ` `return` `total_count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `100` `, M = ` `1` `; ` ` ` ` ` `// Function call ` ` ` `System.out.print(getCount(N, M)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to implement ` `# the above approach ` ` ` `# Function to count the numbers ` `# ending with M ` `def` `getCount(N, M): ` ` ` ` ` `# Stores the count of ` ` ` `# numbers required ` ` ` `total_count ` `=` `0` ` ` ` ` `# Calculate count upto ` ` ` `# nearest power of 10 ` ` ` `total_count ` `+` `=` `N ` `/` `/` `10` ` ` ` ` `# Computing the value of x ` ` ` `x ` `=` `(N ` `/` `/` `10` `) ` `*` `10` ` ` ` ` `# Adding the count of numbers ` ` ` `# ending at M from x to N ` ` ` `if` `((N ` `-` `x) >` `=` `M): ` ` ` `total_count ` `=` `total_count ` `+` `1` ` ` ` ` `return` `total_count ` ` ` `# Driver Code ` `N ` `=` `100` `M ` `=` `1` ` ` `# Function call ` `print` `(getCount(N, M)) ` ` ` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` `class` `GFG{ ` ` ` ` ` `// Function to count the ` ` ` `// numbers ending with M ` ` ` `static` `int` `getCount(` `int` `N, ` `int` `M) ` ` ` `{ ` ` ` ` ` `// Stores the count of ` ` ` `// numbers required ` ` ` `int` `total_count = 0; ` ` ` ` ` `// Calculate count upto ` ` ` `// nearest power of 10 ` ` ` `total_count += (N / 10); ` ` ` ` ` `// Computing the value of x ` ` ` `int` `x = (N / 10) * 10; ` ` ` ` ` `// Adding the count of numbers ` ` ` `// ending at M from x to N ` ` ` `if` `((N - x) >= M) ` ` ` `{ ` ` ` `total_count = total_count + 1; ` ` ` `} ` ` ` `return` `total_count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `N = 100, M = 1; ` ` ` ` ` `// Function call ` ` ` `Console.Write(getCount(N, M)); ` ` ` `} ` `} ` ` ` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output:**

10

**Time complexity: **O(1) **Auxiliary Space:** O(1)

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:

- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of N-digit numbers having digit XOR as single digit
- Last digit of a number raised to last digit of N factorial
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count n digit numbers not having a particular digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- N digit numbers having difference between the first and last digits as K
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Count non-palindromic array elements having same first and last digit
- Find the remainder when First digit of a number is divided by its Last digit
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Count numbers having 0 as a digit
- Count numbers having 0 as a digit
- Count numbers formed by given two digit with sum having given digits
- Count of N digit Numbers having no pair of equal consecutive Digits
- Count three-digit numbers having difference X with its reverse
- Find Last Digit of a^b for Large Numbers
- Last digit of Product of two Large or Small numbers (a * b)
- Last digit of sum of numbers in the given range in the Fibonacci series

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.