Related Articles

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

• Last Updated : 19 May, 2021

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 that lie between n and m that can be evenly divided by their 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 their digits except
2 numbers 10 and 13, which lie between 1 and 15.
10 can be broken into digits 1 and 0.
10 % 1 == 0 but 10 % 0 != 0.
13 can be broken into 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 their digits except
there are 3 numbers; 21, 23 and 25 lie between 21 and 25.
21 can be broken into digits 2 and 1.
21 % 2 != 0 and 21 % 1 != 0
23 can be broken into digits 2 and 3.
23 % 2 != 0 and 23 % 3 != 0.
25 can be broken into 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 a 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 it to the list, else discard it. Finally, return the list.

Below is the implementation of the above approach:

## C++

 `// C++ program to find all the``// possible numbers that can be``// evenly divided by its digits``#include``#include``#include``using` `namespace` `std;` `// Function to check whether the``// number is evenly divisible by``// its digits or not.``bool` `isDivisible(``int` `num)``{``  ``// Iterate each number convert``  ``// number into string and then``  ``// to character array.` `  ``// declaring output string stream``  ``ostringstream str1;` `  ``// Sending a number as a stream``  ``// into output string``  ``str1 << num;` `  ``// the str() coverts number into``  ``// string``  ``string str2 = str1.str();` `  ``for` `(``char` `c : str2 )``  ``{``    ``if` `(c == ``'0'` `||``        ``num % (c - ``'0'``) > 0)``    ``{``      ``return` `false``;``    ``}``  ``}``  ``return` `true``;``}` `// 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``vector<``int``> selfDividingNumber(``int` `left,``                               ``int` `right)``{``  ``vector<``int``>list ;``  ` `  ``for` `(``int` `i = left; i <= right; i++)``  ``{``    ``if` `(isDivisible(i))``    ``{``      ``list.push_back(i);``    ``}``  ``}``  ``return` `list;``}` `// Driver Code``int` `main()``{``  ``// initialise range``  ``int` `n1 = 1, m1 = 15;` `  ``vector<``int``> ans =``  ``(selfDividingNumber(n1, m1));``  ` `  ``for``(``auto` `i = ans.begin();``           ``i != ans.end(); i++)``    ``cout << (*i) << ``" "``;``}` `// This code is contributed by Chitranayal`

## 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 selfDividingNumber(``int` `left,``                                            ``int` `right)``    ``{` `        ``List list = ``new` `ArrayList();``        ``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));``    ``}``}`

## 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`

## 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`

## Javascript

 ``
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.  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