Given two integers n and m which represent a range where n is the lower bound and m is the upper bound respectively. The task is to find all the possible numbers lie between n and m that can be evenly divided by its digits.

**Examples:**

Input:n = 1, m = 15

Output:[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]

Explanation:

The numbers in the array can be divided evenly by its digits except

2 numbers 10 and 13 which lie between 1 and 15.

10 can be broken into its digits 1 and 0.

10 % 1 == 0 but 10 % 0 != 0.

13 can be broken into its digits 1 and 3.

13 % 1 == 0 but 13 % 3 != 0.

Input:n = 21, m = 25

Output:[22, 24]

Explanation:

The numbers in the array can be divided evenly by its digits except

there are 3 numbers 21, 23 and 25 lie between 21 and 25.

21 can be broken into its digits 2 and 1.

21 % 2 != 0 and 21 % 1 != 0

23 can be broken into its digits 2 and 3.

23 % 2 != 0 and 23 % 3 != 0.

25 can be broken into its digits 2 and 5.

25 % 2 != 0 but 25 % 5 == 0.

**Approach:**

The main idea is to iterate each and every number from left to right. For each number convert it into the string followed by a character array and check whether it contains 0 if it contains 0 ignore it. Otherwise, for each number check, whether the number is evenly divided by its digits or not. If the number is evenly divided by its digits then add into the list else discard it. Finally, return the list.

Below is the implementation of the above approach:

## Java

`// Java program to find all the possible numbers ` `// that can be evenly divided by its digits ` ` ` `import` `java.util.*; ` `class` `GFG { ` ` ` ` ` `// Function to check each and every number ` ` ` `// from left to right. If the number is ` ` ` `// divisible by its digits ` ` ` `// then the number is added into the list ` ` ` `static` `List<Integer> selfDividingNumber(` `int` `left, ` ` ` `int` `right) ` ` ` `{ ` ` ` ` ` `List<Integer> list = ` `new` `ArrayList<Integer>(); ` ` ` `for` `(` `int` `i = left; i <= right; i++) { ` ` ` `if` `(isDivisible(i)) { ` ` ` `list.add(i); ` ` ` `} ` ` ` `} ` ` ` `return` `list; ` ` ` `} ` ` ` ` ` `// Function to check whether the number ` ` ` `// is evenly divisible by its digits or not. ` ` ` `static` `boolean` `isDivisible(` `int` `num) ` ` ` `{ ` ` ` ` ` `// Iterate each number convert number ` ` ` `// into string and then to character array. ` ` ` `for` `(` `char` `c : String.valueOf(num).toCharArray()) { ` ` ` `if` `(c == ` `'0'` `|| num % (c - ` `'0'` `) > ` `0` `) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `// initialise range ` ` ` `int` `n1 = ` `1` `, m1 = ` `15` `; ` ` ` ` ` `System.out.print(selfDividingNumber(n1, m1)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find all the possible numbers ` `# that can be evenly divided by its digits ` ` ` `# Function to check each and every number ` `# from left to right. If the number is ` `# divisible by its digits ` `# then the number is added into the list ` `def` `selfDividingNumber(left, right) : ` ` ` `array_list ` `=` `[]; ` ` ` ` ` `for` `i ` `in` `range` `(left, right ` `+` `1` `) : ` ` ` `if` `(isDivisible(i)) : ` ` ` `array_list.append(i); ` ` ` ` ` `return` `array_list; ` ` ` `# Function to check whether the number ` `# is evenly divisible by its digits or not. ` `def` `isDivisible(num) : ` ` ` ` ` `# Iterate each number convert number ` ` ` `# into string and then to character array. ` ` ` `for` `c ` `in` `list` `(` `str` `(num)) : ` ` ` `if` `(c ` `=` `=` `'0'` `or` `num ` `%` `(` `ord` `(c) ` `-` `ord` `(` `'0'` `)) > ` `0` `): ` ` ` `return` `False` `; ` ` ` ` ` `return` `True` `; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Initialise range ` ` ` `n1 ` `=` `1` `; m1 ` `=` `15` `; ` ` ` `print` `(selfDividingNumber(n1, m1)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find all the ` `// possible numbers that can ` `// be evenly divided by its digits ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `public` `class` `GFG { ` ` ` `// Function to check each and every number ` `// from left to right. If the number is ` `// divisible by its digits then the number ` `// is added into the list ` `static` `List<` `int` `> selfDividingNumber(` `int` `left, ` ` ` `int` `right) ` `{ ` ` ` `List<` `int` `> list = ` `new` `List<` `int` `>(); ` ` ` `for` `(` `int` `i = left; i <= right; i++) ` ` ` `{ ` ` ` `if` `(isDivisible(i)) ` ` ` `{ ` ` ` `list.Add(i); ` ` ` `} ` ` ` `} ` ` ` `return` `list; ` `} ` ` ` `// Function to check whether the number ` `// is evenly divisible by its digits or not. ` `static` `bool` `isDivisible(` `int` `num) ` `{ ` ` ` ` ` `// Iterate each number convert number ` ` ` `// into string and then to character array. ` ` ` `foreach` `(` `char` `c ` `in` `String.Join(` `""` `, num).ToCharArray()) ` ` ` `{ ` ` ` `if` `(c == ` `'0'` `|| num % (c - ` `'0'` `) > 0) ` ` ` `{ ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` ` ` `// Initialise range ` ` ` `int` `n1 = 1, m1 = 15; ` ` ` `List<` `int` `> t = selfDividingNumber(n1, m1); ` ` ` ` ` `foreach` `(` `int` `val ` `in` `t) ` ` ` `Console.Write(val + ` `", "` `); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]

**Time Complexity:** O(N), where N is the number of integers from left to right.

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:

- Minimum squares to evenly cut a rectangle
- How to evenly put N objects into N places with adjacent moves
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Find the remainder when First digit of a number is divided by its Last digit
- Check if the sum of digits of number is divisible by all of its digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Minimum digits to be removed to make either all digits or alternating digits same
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Smallest N digit number with none of its digits as its divisor
- Maximize the value of A by replacing some of its digits with digits of B
- Sum of the digits of square of the given number which has only 1's as its digits
- First N natural can be divided into two sets with given difference and co-prime sums
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Check if an array of 1s and 2s can be divided into 2 parts with equal 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.