Given three integers the lower range **L** ,the upper range **U** and a digit **M**. The task is to count all the numbers between L and U such that the number is divisible by M and also, it does not contain the digit M.**Examples:**

Input:M = 9 ,L = 16 , U = 26Output:1Explanation:Within this given range ,the number that follows the above two given conditions is: 18 .Input:M = 6 ,L = 88 , U = 102Output:2Explanation:Within this given range ,the numbers that follows the above two given conditions are: 90 and 102 .

**Approach: **

- The idea is to iterate from the lower range(L) to the upper range(U) and for each number,

- we will store the distinct digits of the number in a
variable and will check if the set contains the digit M or not as per the given conditions. If the number does not contain the given digit M and is divisible by M, then the counter is incremented by 1.**num**

## C++

`// C++ implementation to illustrate ` `// the program ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count all the numbers ` `// which does not contain the digit 'M' ` `// and is divisible by M ` `void` `contain(` `int` `L, ` `int` `U, ` `int` `M) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `j = L; j < U; j++) ` ` ` `{ ` ` ` ` ` `// Storing all the distinct ` ` ` `// digits of a number ` ` ` `set<string> num; ` ` ` `string str = to_string(j); ` ` ` `num.insert(str); ` ` ` ` ` `// Checking if the two conditions ` ` ` `// are satisfied or not ` ` ` `if` `(j % M == 0 and ` ` ` `num.find(to_string(M)) == num.end()) ` ` ` `{ ` ` ` `count += 1; ` ` ` `} ` ` ` `} ` ` ` `cout << count - 2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Lower Range ` ` ` `int` `L = 106; ` ` ` ` ` `// Upper Range ` ` ` `int` `U = 200; ` ` ` ` ` `// The digit ` ` ` `int` `M = 7; ` ` ` ` ` `contain(L, U, M); ` `} ` ` ` `// This code is contributed by BhupendraSingh ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to illustrate ` `// the program ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count all the numbers ` `// which does not contain the digit 'M' ` `// and is divisible by M ` `static` `void` `contain(` `int` `L, ` `int` `U, ` `int` `M) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `j = L; j < U; j++) ` ` ` `{ ` ` ` ` ` `// Storing all the distinct ` ` ` `// digits of a number ` ` ` `HashSet<String> num = ` `new` `HashSet<>(); ` ` ` `String str = Integer.toString(j); ` ` ` `num.add(str); ` ` ` ` ` `// Checking if the two conditions ` ` ` `// are satisfied or not ` ` ` `if` `(j % M == ` `0` `&& !num.contains( ` ` ` `Integer.toString(M))) ` ` ` `{ ` ` ` `count += ` `1` `; ` ` ` `} ` ` ` `} ` ` ` `System.out.println(count - ` `2` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Lower Range ` ` ` `int` `L = ` `106` `; ` ` ` ` ` `// Upper Range ` ` ` `int` `U = ` `200` `; ` ` ` ` ` `// The digit ` ` ` `int` `M = ` `7` `; ` ` ` ` ` `contain(L, U, M); ` `} ` `} ` ` ` `// This code is contributed by jrishabh99 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to illustrate ` `# the program ` ` ` `# Function to count all the numbers ` `# which does not contain the digit 'M' ` `# and is divisible by M ` `def` `contain (L,U,M): ` ` ` `count ` `=` `0` ` ` `for` `j ` `in` `range` `(L,U` `+` `1` `): ` ` ` ` ` `# Storing all the distinct ` ` ` `# digits of a number ` ` ` `num ` `=` `set` `(` `str` `(j)) ` ` ` ` ` `# Checking if the two conditions ` ` ` `# are satisfied or not ` ` ` `if` `(j ` `%` `M ` `=` `=` `0` `and` `str` `(M) ` `not` `in` `num): ` ` ` `count ` `+` `=` `1` ` ` `print` `(count) ` ` ` `#Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` ` ` `L ` `=` `106` `# Lower Range ` ` ` `U ` `=` `200` `# Upper Range ` ` ` `M ` `=` `7` `# The digit ` ` ` ` ` `contain(L,U,M) ` ` ` `# This code is contributed by parna_28 ` |

*chevron_right*

*filter_none*

**Output:**

11

## Recommended Posts:

- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Count of Numbers in Range where first digit is equal to last digit of the number
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count of Numbers in a Range where digit d occurs exactly K times
- Count numbers in a range that are divisible by all array elements
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Find two numbers made up of a given digit such that their difference is divisible by N
- Numbers that are not divisible by any number in the range [2, 10]
- No of pairs (a[j] >= a[i]) with k numbers in range (a[i], a[j]) that are divisible by x
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Queries for the count of even digit sum elements in the given range using Segment Tree.
- Count integers in a range which are divisible by their euler totient value
- Count of numbers upto M divisible by given Prime Numbers
- Count numbers from 1 to n that have 4 as a digit
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1
- Count numbers with exactly K non-zero digits and distinct odd digit sum

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.