Given an integer **N**, the task is to find any N-digit positive number (except for zeros) such that it is not divisible by any of its digits. If it is not possible to find any such number then print **-1**.

**Note:** There can be more than one such number for the same N-digit.

**Examples:**

Input:N = 2Output:23 23 is not divisible by 2 or 3Input:N = 3Output:239

**Approach:**

The easiest solution to this problem can be thought of with the help of digits ‘4’ and ‘5’.

- Since, in order for a number to be divisible by 5, the number must end with 0 or 5; and in order for it to be divisible by 4, the last two digits if the number must be divisible by 4.
- Therefore, a shortcut method can be applied to prevent both of the divisibility criteria of 4 and as well as of 5, as:
- To prevent a number from being divisible by 5, the number can contain 5 for every other digit except for last digit.
Therefore for N digit number, (N - 1) digits must be 5 = 5555...(N-1 times)d where d is the Nth digit

- To prevent a number from being divisible by 4, the number can contain 5 at the second last digit and 4 at the last digit.
Therefore for N digit number, Last digit must be 4 = 5555...(N-1 times)4

- To prevent a number from being divisible by 5, the number can contain 5 for every other digit except for last digit.

Below is the implementation of the above approach:

## CPP

`// CPP program to find N digit number such ` `// that it is not divisible by any of its digits ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that print the answer ` `void` `findTheNumber(` `int` `n) ` `{ ` ` ` `// if n == 1 then it is ` ` ` `// not possible ` ` ` `if` `(n == 1) { ` ` ` `cout << ` `"Impossible"` `<< endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// loop to n-1 times ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` `cout << ` `"5"` `; ` ` ` `} ` ` ` ` ` `// print 4 as last digit of ` ` ` `// the number ` ` ` `cout << ` `"4"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 12; ` ` ` ` ` `// Function call ` ` ` `findTheNumber(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA program to find N digit number such ` `// that it is not divisible by any of its digits ` `class` `GFG{ ` ` ` `// Function that print the answer ` `static` `void` `findTheNumber(` `int` `n) ` `{ ` ` ` `// if n == 1 then it is ` ` ` `// not possible ` ` ` `if` `(n == ` `1` `) { ` ` ` `System.out.print(` `"Impossible"` `+` `"\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// loop to n-1 times ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) { ` ` ` `System.out.print(` `"5"` `); ` ` ` `} ` ` ` ` ` `// print 4 as last digit of ` ` ` `// the number ` ` ` `System.out.print(` `"4"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `12` `; ` ` ` ` ` `// Function call ` ` ` `findTheNumber(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find N digit number such ` `# that it is not divisible by any of its digits ` ` ` `# Function that prthe answer ` `def` `findTheNumber(n): ` ` ` `# if n == 1 then it is ` ` ` `# not possible ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `print` `(` `"Impossible"` `) ` ` ` `return` ` ` ` ` `# loop to n-1 times ` ` ` `for` `i ` `in` `range` `(n` `-` `1` `): ` ` ` `print` `(` `"5"` `,end` `=` `"") ` ` ` ` ` `# print as last digit of ` ` ` `# the number ` ` ` `print` `(` `"4"` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `12` ` ` ` ` `#Function call ` ` ` `findTheNumber(n) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find N digit number such ` `// that it is not divisible by any of its digits ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that print the answer ` `static` `void` `findTheNumber(` `int` `n) ` `{ ` ` ` `// if n == 1 then it is ` ` ` `// not possible ` ` ` `if` `(n == 1) { ` ` ` `Console.Write(` `"Impossible"` `+` `"\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// loop to n-1 times ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` `Console.Write(` `"5"` `); ` ` ` `} ` ` ` ` ` `// print 4 as last digit of ` ` ` `// the number ` ` ` `Console.Write(` `"4"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 12; ` ` ` ` ` `// Function call ` ` ` `findTheNumber(n); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

555555555554

**Time complexity:** **0(N)**

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:

- Find N digits number which is divisible by D
- Find a N-digit number such that it is not divisible by any of its digits
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Check if the sum of digits of number is divisible by all of its digits
- Smallest number with sum of digits as N and divisible by 10^N
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Number of digits to be removed to make a number divisible by 3
- Check if N is divisible by a number which is composed of the digits from the set {A, B}
- Possible to make a divisible by 3 number using all digits in an array
- Program to check if a number is divisible by sum of its digits
- Check whether sum of digits at odd places of a number is divisible by K
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Program to check if a number is divisible by any of its digits
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Check whether product of digits at even places of a number is divisible by K
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Given a large number, check if a subsequence of digits is divisible by 8
- Smallest N digit number divisible by all possible prime 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.