Here given a range from low to high and given a number k.You have to find out the number of count which a number has same digit as k**Examples:**

Input:low = 2, high = 35, k = 2Output:4 Numbers are 2, 12, 22, 32Input:low = 3, high = 30, k = 3Output:3 Numbers are 3, 13, 23

A **naive approach** is to traverse through all numbers in given range and check last digit of every number and increment result if last digit is equal to k.

## C++

`// Simple CPP program to count numbers with ` `// last digit as k in given range.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Returns count of numbers with k as last` `// digit.` `int` `counLastDigitK(` `int` `low, ` `int` `high, ` `int` `k)` `{` ` ` `int` `count = 0;` ` ` `for` `(` `int` `i = low; i <= high; i++) ` ` ` `if` `(i % 10 == k)` ` ` `count++; ` ` ` `return` `count;` `}` `// Driver Program` `int` `main()` `{` ` ` `int` `low = 3, high = 35, k = 3;` ` ` `cout << counLastDigitK(low, high, k);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Simple Java program to count numbers with ` `// last digit as k in given range.` `import` `java.util.*;` `import` `java.lang.*;` `public` `class` `GfG` `{` ` ` `// Returns count of numbers with` ` ` `// k as last digit.` ` ` `public` `static` `int` `counLastDigitK(` `int` `low, ` ` ` `int` `high, ` `int` `k)` ` ` `{` ` ` `int` `count = ` `0` `;` ` ` `for` `(` `int` `i = low; i <= high; i++) ` ` ` `if` `(i % ` `10` `== k)` ` ` `count++; ` ` ` `return` `count;` ` ` `}` ` ` ` ` `// driver function` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `int` `low = ` `3` `, high = ` `35` `, k = ` `3` `;` ` ` `System.out.println(counLastDigitK(low, high, k));` ` ` `}` `}` `// This code is contributed by Sagar Shukla` |

*chevron_right*

*filter_none*

## Python3

`# Simple python program to count numbers with ` `# last digit as k in given range.` `# Returns count of numbers with k as last` `# digit.` `def` `counLastDigitK(low, high, k):` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(low, high` `+` `1` `):` ` ` `if` `(i ` `%` `10` `=` `=` `k):` ` ` `count` `+` `=` `1` ` ` `return` `count` `# Driver Program` `low ` `=` `3` `high ` `=` `35` `k ` `=` `3` `print` `(counLastDigitK(low, high, k))` `# This code is contributed by` `# Smitha Dinesh Semwal` |

*chevron_right*

*filter_none*

## C#

`// Simple C# program to count numbers with ` `// last digit as k in given range.` `using` `System;` `public` `class` `GfG` `{` ` ` `// Returns count of numbers with` ` ` `// k as last digit.` ` ` `public` `static` `int` `counLastDigitK(` `int` `low, ` ` ` `int` `high, ` `int` `k)` ` ` `{` ` ` `int` `count = 0;` ` ` `for` `(` `int` `i = low; i <= high; i++) ` ` ` `if` `(i % 10 == k)` ` ` `count++; ` ` ` `return` `count;` ` ` `}` ` ` ` ` `// Driver function` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `low = 3, high = 35, k = 3;` ` ` `Console.WriteLine(counLastDigitK(low, high, k));` ` ` `}` `}` `// This code is contributed by vt_m` |

*chevron_right*

*filter_none*

## PHP

`<?php` `// Simple PHP program to count numbers with ` `// last digit as k in given range.` `// Returns count of numbers with` `// k as last digit.` `function` `counLastDigitK(` `$low` `, ` `$high` `, ` `$k` `)` `{` ` ` `$count` `= 0;` ` ` `for` `(` `$i` `= ` `$low` `; ` `$i` `<= ` `$high` `; ` `$i` `++) ` ` ` `if` `(` `$i` `% 10 == ` `$k` `)` ` ` `$count` `++; ` ` ` `return` `$count` `;` `}` ` ` `// Driver Code` ` ` `$low` `= 3;` ` ` `$high` `= 35; ` ` ` `$k` `= 3;` ` ` `echo` `counLastDigitK(` `$low` `, ` `$high` `, ` `$k` `);` ` ` `// This code is contributed by ajit` `?>` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(high – low)

An **efficient solution** is based on the fact that every digit appears once as the last digit in every 10 consecutive numbers.

## C++

`// Efficient CPP program to count numbers ` `// with last digit as k in given range.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Returns count of numbers with k as last ` `// digit. ` `int` `countLastDigitK(` `long` `long` `low, ` ` ` `long` `long` `high, ` `long` `long` `K) ` `{` ` ` `long` `long` `mlow = 10 * ` `ceil` `(low/10.0);` ` ` `long` `long` `mhigh = 10 * ` `floor` `(high/10.0);` ` ` `int` `count = (mhigh - mlow)/10; ` ` ` `if` `(high % 10 >= K) ` ` ` `count++; ` ` ` `if` `(low % 10 <=K && (low%10))` ` ` `count++;` ` ` `return` `count; ` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `low = 3, high = 35, k = 3;` ` ` `cout << countLastDigitK(low, high, k);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Efficient Java program to count numbers ` `// with last digit as k in given range.` `import` `java.util.*;` `import` `java.lang.*;` `public` `class` `GfG` `{` ` ` `// Returns count of numbers with ` ` ` `// k as last digit.` ` ` `public` `static` `int` `counLastDigitK(` `int` `low, ` ` ` `int` `high, ` `int` `k)` ` ` `{` ` ` `int` `mlow = ` `10` `* (` `int` `) ` ` ` `Math.ceil(low/` `10.0` `);` ` ` `int` `mhigh = ` `10` `* (` `int` `) ` ` ` `Math.floor(high/` `10.0` `);` ` ` `int` `count = (mhigh - mlow)/` `10` `;` ` ` `if` `(high % ` `10` `>= k)` ` ` `count++;` ` ` `if` `(low % ` `10` `<= k && (low%` `10` `) > ` `0` `)` ` ` `count++;` ` ` `return` `count;` ` ` `}` ` ` ` ` `// driver function` ` ` `public` `static` `void` `main(String argc[])` ` ` `{` ` ` `int` `low = ` `3` `, high = ` `35` `, k = ` `3` `;` ` ` `System.out.println(counLastDigitK(low, high, k));` ` ` `}` `}` `// This code is contributed by Sagar Shukla` |

*chevron_right*

*filter_none*

## Python3

`import` `math` `# Efficient python program to count numbers ` `# with last digit as k in given range.` `# Returns count of numbers with k as last` `# digit.` `def` `counLastDigitK(low, high, k):` ` ` `mlow ` `=` `10` `*` `math.ceil(low` `/` `10.0` `)` ` ` `mhigh ` `=` `10` `*` `int` `(high` `/` `10.0` `)` ` ` ` ` `count ` `=` `(mhigh ` `-` `mlow)` `/` `10` ` ` `if` `(high ` `%` `10` `>` `=` `k):` ` ` `count ` `+` `=` `1` ` ` `if` `(low ` `%` `10` `<` `=` `k ` `and` `\` ` ` `(low` `%` `10` `) > ` `0` `):` ` ` `count ` `+` `=` `1` ` ` `return` `int` `(count)` `# Driver Code` `low ` `=` `3` `high ` `=` `35` `k ` `=` `3` `print` `(counLastDigitK(low, high, k))` `# This code is contributed by` `# Smitha Dinesh Semwal` |

*chevron_right*

*filter_none*

## C#

`// Efficient Java program to count numbers ` `// with last digit as k in given range.` `using` `System;` `public` `class` `GfG` `{` ` ` `// Returns count of numbers with ` ` ` `// k as last digit.` ` ` `public` `static` `int` `counLastDigitK(` `int` `low, ` ` ` `int` `high, ` `int` `k)` ` ` `{` ` ` `int` `mlow = 10 * Convert.ToInt32(` ` ` `Math.Ceiling(low/10.0));` ` ` `int` `mhigh = 10 * Convert.ToInt32(` ` ` `Math.Floor(high/10.0));` ` ` `int` `count = (mhigh - mlow) / 10;` ` ` `if` `(high % 10 >= k)` ` ` `count++;` ` ` `if` `(low % 10 <= k && (low%10) > 0)` ` ` `count++;` ` ` `return` `count;` ` ` `}` ` ` ` ` `// Driver function` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `low = 3, high = 35, k = 3;` ` ` `Console.WriteLine(` ` ` `counLastDigitK(low, high, k));` ` ` `}` `}` `// This code is contributed by vt_m` |

*chevron_right*

*filter_none*

**Output**

4

**Time Complexity :** 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 of N-digit numbers having digit XOR as single digit
- Minimum count of numbers required with unit digit X that sums up to N
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Sum of integers upto N with given unit digit
- Sum of integers upto N with given unit digit (Set 2)
- Print multiples of Unit Digit of Given Number
- Count n digit numbers not having a particular digit
- Find unit digit of x raised to power y
- Find the unit place digit of sum of N factorials
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Last digit of sum of numbers in the given range in the Fibonacci series
- Count of Equilateral Triangles of unit length possible from a given Hexagon
- Count 'd' digit positive integers with 0 as a digit
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Count numbers formed by given two digit with sum having given digits

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.