# Find all the possible numbers in a range that can be evenly divided by its digits

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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- 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
- Count of numbers from range [L, R] that end with any of the given digits
- Count of numbers from range [L, R] whose sum of digits is Y
- Count of numbers with all digits same in a given range
- Total numbers with no repeated digits in a range
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Cumulative product of digits of all numbers in the given range
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Find the number in a range having maximum product of the digits
- Find the numbers from 1 to N that contains exactly k non-zero digits
- Find all palindrome numbers of given digits
- Count all prime numbers in a given range whose sum of digits is also prime
- Find all Autobiographical Numbers with given number of digits
- Find all the prime numbers of given number of digits
- Find XOR of numbers from the range [L, R]
- Find N numbers such that a number and its reverse are divisible by sum of its 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.