Count of numbers from range [L, R] that end with any of the given digits Last Updated : 28 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 2 and 3 are the only valid numbers.Input: L = 11, R = 33 Output: 8 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 <iostream> 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 <script> // JavaScript implementation of the approach // Function to return the count // of the required numbers function countNums(l, r) { let cnt = 0; for (let i = l; i <= r; i++) { // Last digit of the current number let 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; } let l = 11, r = 33; document.write(countNums(l, r)); </script> Output: 8 Time Complexity: O(r) Auxiliary Space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Count numbers from a given range whose product of digits is K C coodieaniket Follow Improve Article Tags : DSA Similar Reads Count of numbers from the range [L, R] which contains at least one digit that divides K Given three positive integers L, R and K.The task is to find the count of all the numbers from the range [L, R] that contains at least one digit which divides the number K. Examples: Input: L = 5, R = 11, K = 10 Output: 3 5, 10 and 11 are only such numbers. Input: L = 32, R = 38, K = 13 Output: 0 Ap 6 min read Count of numbers with all digits same in a given range 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 ra 9 min read Count numbers from a given range whose product of digits is K Given three positive integers L, R and K, the task is to count the numbers in the range [L, R] whose product of digits is equal to K Examples: Input: L = 1, R = 130, K = 14Output: 3Explanation: Numbers in the range [1, 100] whose sum of digits is K(= 14) are: 27 => 2 * 7 = 14 72 => 7 * 2 = 14 15+ min read Queries to count numbers from a given range which are divisible the sum of their digits Given an array Q[][] consisting of N queries of the form {L, R}, the task for each query is to find the total count of the numbers from the range [L, R] which are divisible by the sum of their digits. Examples: Input: Q[][]= {{5, 9}, {5, 20}}Output: 59Explanation: Query 1: The numbers in the range [ 8 min read Count numbers from a given range that are not divisible by any of the array elements Given an array arr[] consisting of N positive integers and integers L and R, the task is to find the count of numbers in the range [L, R] which are not divisible by any of the array elements. Examples: Input: arr[] = {2, 3, 4, 5, 6}, L = 1, R = 20Output: 6Explanation:The 6 numbers in the range [1, 2 7 min read Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 Given two integers L and R. The task is to find the count of all even numbers in the range [L, R] whose sum of digits is divisible by 3.Examples: Input: L = 18, R = 36 Output: 4 18, 24, 30, 36 are the only numbers in the range [18, 36] which are even and whose sum of digits is divisible by 3.Input: 8 min read Like