# Smallest n digit number divisible by given three numbers

Given x, y, z and n, find smallest n digit number which is divisible by x, y and z.

Examples:

```Input : x = 2, y = 3, z = 5
n = 4
Output : 1020

Input : x = 3, y = 5, z = 7
n = 2
Output : Not possible
```

## Recommended: Please solve it on PRACTICE first, before moving on to the solution.

1) Find smallest n digit number is pow(10, n-1).
2) Find LCM of given 3 numbers x, y and z.
3) Find remainder of the LCM when divided by pow(10, n-1).
4) Add the “LCM – remainder” to pow(10, n-1). If this addition is still a n digit number, we return the result. Else we return Not possible.

Illustration :
Suppose n = 4 and x, y, z are 2, 3, 5 respectively.
1) First find the least four digit number i.e. 1000,
2) LCM of 2, 3, 5 so the LCM is 30.
3) Find the reminder of 1000 % 30 = 10
4) Subtract the remainder from LCM, 30 – 10 = 20. Result is 1000 + 20 = 1020.

Below is the implementation of above approach:

## C++

 `// C++ program to find smallest n digit number ` `// which is divisible by x, y and z. ` `#include ` `using` `namespace` `std; ` ` `  `// LCM for x, y, z ` `int` `LCM(``int` `x, ``int` `y, ``int` `z) ` `{ ` `    ``int` `ans = ((x * y) / (__gcd(x, y))); ` `    ``return` `((z * ans) / (__gcd(ans, z))); ` `} ` ` `  `// returns smallest n digit number divisible ` `// by x, y and z ` `int` `findDivisible(``int` `n, ``int` `x, ``int` `y, ``int` `z) ` `{ ` `    ``// find the LCM ` `    ``int` `lcm = LCM(x, y, z); ` ` `  `    ``// find power of 10 for least number ` `    ``int` `ndigitnumber = ``pow``(10, n-1); ` `     `  `    ``// reminder after ` `    ``int` `reminder = ndigitnumber % lcm; ` ` `  `    ``// If smallest number itself divides ` `    ``// lcm. ` `    ``if` `(reminder == 0) ` `         ``return` `ndigitnumber; ` ` `  `    ``// add lcm- reminder number for ` `    ``// next n digit number ` `    ``ndigitnumber += lcm - reminder; ` ` `  `    ``// this condition check the n digit ` `    ``// number is possible or not ` `    ``// if it is possible it return  ` `    ``// the number else return 0 ` `    ``if` `(ndigitnumber < ``pow``(10, n)) ` `        ``return` `ndigitnumber; ` `    ``else` `        ``return` `0; ` `} ` ` `  `// driver code ` `int` `main() ` `{ ` `    ``int` `n = 4, x = 2, y = 3, z = 5; ` `    ``int` `res = findDivisible(n, x, y, z); ` ` `  `    ``// if number is possible then  ` `    ``// it print the number ` `    ``if` `(res != 0) ` `        ``cout << res; ` `    ``else` `        ``cout << ``"Not possible"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find smallest n digit number ` `// which is divisible by x, y and z. ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``static` `int` `__gcd(``int` `a, ``int` `b) ` `    ``{ ` ` `  `        ``if` `(b == ``0``) { ` `            ``return` `a; ` `        ``} ` `        ``else` `{ ` `            ``return` `__gcd(b, a % b); ` `        ``} ` `    ``} ` ` `  `    ``// LCM for x, y, z ` `    ``static` `int` `LCM(``int` `x, ``int` `y, ``int` `z) ` `    ``{ ` `        ``int` `ans = ((x * y) / (__gcd(x, y))); ` `        ``return` `((z * ans) / (__gcd(ans, z))); ` `    ``} ` ` `  `    ``// returns smallest n digit number  ` `    ``// divisible by x, y and z ` `    ``static` `int` `findDivisible(``int` `n, ``int` `x,  ` `                                  ``int` `y, ``int` `z) ` `    ``{ ` `         `  `        ``// find the LCM ` `        ``int` `lcm = LCM(x, y, z); ` ` `  `        ``// find power of 10 for least number ` `        ``int` `ndigitnumber = (``int``)Math.pow(``10``, n - ``1``); ` ` `  `        ``// reminder after ` `        ``int` `reminder = ndigitnumber % lcm; ` ` `  `        ``// If smallest number itself divides ` `        ``// lcm. ` `        ``if` `(reminder == ``0``) ` `            ``return` `ndigitnumber; ` ` `  `        ``// add lcm- reminder number for ` `        ``// next n digit number ` `        ``ndigitnumber += lcm - reminder; ` ` `  `        ``// this condition check the n digit ` `        ``// number is possible or not ` `        ``// if it is possible it return ` `        ``// the number else return 0 ` `        ``if` `(ndigitnumber < Math.pow(``10``, n)) ` `            ``return` `ndigitnumber; ` `        ``else` `            ``return` `0``; ` `    ``} ` ` `  `    ``// driver code ` `    ``static` `public` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``int` `n = ``4``, x = ``2``, y = ``3``, z = ``5``; ` `        ``int` `res = findDivisible(n, x, y, z); ` ` `  `        ``// if number is possible then ` `        ``// it print the number ` `        ``if` `(res != ``0``) ` `            ``System.out.println(res); ` `        ``else` `            ``System.out.println(``"Not possible"``); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python3

 `# Python3 code to find smallest n digit  ` `# number which is divisible by x, y and z. ` `from` `fractions ``import` `gcd ` `import` `math ` ` `  `# LCM for x, y, z ` `def` `LCM( x , y , z ): ` `    ``ans ``=` `int``((x ``*` `y) ``/` `(gcd(x, y))) ` `    ``return` `int``((z ``*` `ans) ``/` `(gcd(ans, z))) ` `     `  `# returns smallest n digit number  ` `# divisible by x, y and z ` `def` `findDivisible (n, x, y, z): ` `     `  `    ``# find the LCM ` `    ``lcm ``=` `LCM(x, y, z) ` `     `  `    ``# find power of 10 for least number ` `    ``ndigitnumber ``=` `math.``pow``(``10``, n``-``1``) ` `     `  `    ``# reminder after ` `    ``reminder ``=` `ndigitnumber ``%` `lcm ` `     `  `    ``# If smallest number itself  ` `    ``# divides lcm. ` `    ``if` `reminder ``=``=` `0``: ` `        ``return` `ndigitnumber ` `         `  `    ``# add lcm- reminder number for ` `    ``# next n digit number ` `    ``ndigitnumber ``+``=` `lcm ``-` `reminder ` `     `  `    ``# this condition check the n digit ` `    ``# number is possible or not ` `    ``# if it is possible it return ` `    ``# the number else return 0 ` `    ``if` `ndigitnumber < math.``pow``(``10``, n): ` `        ``return` `int``(ndigitnumber) ` `    ``else``: ` `        ``return` `0` ` `  `# driver code ` `n ``=` `4` `x ``=` `2` `y ``=` `3` `z ``=` `5` `res ``=` `findDivisible(n, x, y, z) ` ` `  `# if number is possible then  ` `# it print the number ` `if` `res !``=` `0``: ` `    ``print``( res) ` `else``: ` `    ``print``(``"Not possible"``) ` `     `  `# This code is contributed by "Sharad_Bhardwaj".  `

## C#

 `// C# program to find smallest n digit number ` `// which is divisible by x, y and z. ` `using` `System; ` ` `  `public` `class` `GFG ` `{ ` `     `  `    ``static` `int` `__gcd(``int` `a, ``int` `b) ` `        ``{ ` `         `  `            ``if``(b == 0)  ` `            ``{ ` `                ``return` `a; ` `            ``} ` `            ``else` `            ``{ ` `                ``return` `__gcd(b, a % b); ` `            ``} ` `        ``} ` `     `  `    ``// LCM for x, y, z ` `    ``static` `int` `LCM(``int` `x, ``int` `y, ``int` `z) ` `    ``{ ` `        ``int` `ans = ((x * y) / (__gcd(x, y))); ` `        ``return` `((z * ans) / (__gcd(ans, z))); ` `    ``} ` `     `  `    ``// returns smallest n digit number divisible ` `    ``// by x, y and z ` `    ``static` `int` `findDivisible(``int` `n, ``int` `x, ``int` `y, ``int` `z) ` `    ``{ ` `        ``// find the LCM ` `        ``int` `lcm = LCM(x, y, z); ` `     `  `        ``// find power of 10 for least number ` `        ``int` `ndigitnumber =(``int``)Math. Pow(10, n - 1); ` `         `  `        ``// reminder after ` `        ``int` `reminder = ndigitnumber % lcm; ` `     `  `        ``// If smallest number itself divides ` `        ``// lcm. ` `        ``if` `(reminder == 0) ` `            ``return` `ndigitnumber; ` `     `  `        ``// add lcm- reminder number for ` `        ``// next n digit number ` `        ``ndigitnumber += lcm - reminder; ` `     `  `        ``// this condition check the n digit ` `        ``// number is possible or not ` `        ``// if it is possible it return  ` `        ``// the number else return 0 ` `        ``if` `(ndigitnumber < Math.Pow(10, n)) ` `            ``return` `ndigitnumber; ` `        ``else` `            ``return` `0; ` `    ``} ` `     `  `    ``// Driver code ` ` `  `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `n = 4, x = 2, y = 3, z = 5; ` `        ``int` `res = findDivisible(n, x, y, z); ` `     `  `        ``// if number is possible then  ` `        ``// it print the number ` `        ``if` `(res != 0) ` `            ``Console.WriteLine(res); ` `        ``else` `            ``Console.WriteLine(``"Not possible"``); ` `             `  `    ``} ` `} ` `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```1020
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : Mithun Kumar

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.