Related Articles

# Count of numbers from range [L, R] that end with any of the given digits

• Last Updated : 10 Jun, 2021

Given a range [L, R], the task is to find the count of numbers from the range whose last digit is either 2, 3 or 9.
Examples:

Input: L = 1, R = 3
Output:
2 and 3 are the only valid numbers.
Input: L = 11, R = 33
Output:

Approach: Initialize a counter count = 0 and run a loop for every element from the range, if the current element ends with any of the given digits then increment the count.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the count``// of the required numbers``int` `countNums(``int` `l, ``int` `r)``{``    ``int` `cnt = 0;` `    ``for` `(``int` `i = l; i <= r; i++)``    ``{` `        ``// Last digit of the current number``        ``int` `lastDigit = (i % 10);` `        ``// If the last digit is equal to``        ``// any of the given digits``        ``if` `((lastDigit % 10) == 2 || (lastDigit % 10) == 3``            ``|| (lastDigit % 10) == 9)``        ``{``            ``cnt++;``        ``}``    ``}` `    ``return` `cnt;``}` `// Driver code``int` `main()``{``    ``int` `l = 11, r = 33;``    ``cout << countNums(l, r) ;``}``    ` `// This code is contributed by AnkitRai01`

## Java

 `// Java implementation of the approach``class` `GFG {` `    ``// Function to return the count``    ``// of the required numbers``    ``static` `int` `countNums(``int` `l, ``int` `r)``    ``{``        ``int` `cnt = ``0``;` `        ``for` `(``int` `i = l; i <= r; i++) {` `            ``// Last digit of the current number``            ``int` `lastDigit = (i % ``10``);` `            ``// If the last digit is equal to``            ``// any of the given digits``            ``if` `((lastDigit % ``10``) == ``2` `|| (lastDigit % ``10``) == ``3``                ``|| (lastDigit % ``10``) == ``9``) {``                ``cnt++;``            ``}``        ``}` `        ``return` `cnt;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `l = ``11``, r = ``33``;``        ``System.out.print(countNums(l, r));``    ``}``}`

## Python3

 `# Python3 implementation of the approach` `# Function to return the count``# of the required numbers``def` `countNums(l, r) :``    ``cnt ``=` `0``;` `    ``for` `i ``in` `range``(l, r ``+` `1``) :` `        ``# Last digit of the current number``        ``lastDigit ``=` `(i ``%` `10``);` `        ``# If the last digit is equal to``        ``# any of the given digits``        ``if` `((lastDigit ``%` `10``) ``=``=` `2` `or` `(lastDigit ``%` `10``) ``=``=` `3``            ``or` `(lastDigit ``%` `10``) ``=``=` `9``) :``        ` `            ``cnt ``+``=` `1``;` `    ``return` `cnt;` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:``    ` `    ``l ``=` `11``; r ``=` `33``;``    ` `    ``print``(countNums(l, r)) ;` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{` `    ``// Function to return the count``    ``// of the required numbers``    ``static` `int` `countNums(``int` `l, ``int` `r)``    ``{``        ``int` `cnt = 0;` `        ``for` `(``int` `i = l; i <= r; i++)``        ``{` `            ``// Last digit of the current number``            ``int` `lastDigit = (i % 10);` `            ``// If the last digit is equal to``            ``// any of the given digits``            ``if` `((lastDigit % 10) == 2 ||``                ``(lastDigit % 10) == 3 ||``                ``(lastDigit % 10) == 9)``            ``{``                ``cnt++;``            ``}``        ``}``        ``return` `cnt;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int` `l = 11, r = 33;``        ``Console.Write(countNums(l, r));``    ``}``}` `// This code is contributed by AnkitRai01`

## Javascript

 ``
Output:
`8`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up