Given an integer N, the task is to find an N-digit number such that it is not divisible by any of its digits.

**Note:** There can be multiple answers for each value of N.

**Examples:**

Input:N = 4

Output:6789

Explanation:

As the number 6789 is not divisible by any of its digits that is 6, 7, 8 and 9 and it is also a four digit number, Hence it can be the desired number.

Input:N = 2

Output:57

Explanation:

As the number 57 is not divisible by any of its digits that is 5 and 7 and it is also a 2-digit number, Hence it can be the desired number.

**Approach:** The key observation in the problem is that 2 and 3 are those numbers which don’t divide each other also the numbers “23, 233, 2333, …” are not divisible by neither 2 nor 3. Hence, for any N-digit number, the most-significant digit will be 2 and the rest of the digits will be 3 to get the desired number.

**Algorithm:**

- Check if the value of the N is equal to 1, then there is no such number is possible hence return -1.
- Otherwise initialize a variable
**num**, to store the number by 2. - Run a loop from 1 to N and then for each iteration multiply the number by 10 and add 3 to it.
num = (num * 10) + 3

Below is the implementation of the above approach:

## C++

`// C++ implementation to find a ` `// N-digit number such that the number ` `// it is not divisible by its digits ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `typedef` `long` `long` `int` `ll; ` ` ` `// Function to find the number ` `// such that it is not divisible ` `// by its digits ` `void` `solve(ll n) ` `{ ` ` ` `// Base Cases ` ` ` `if` `(n == 1) ` ` ` `{ ` ` ` `cout << -1; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// First Digit of the ` ` ` `// number will be 2 ` ` ` `int` `num = 2; ` ` ` ` ` `// Next digits of the numbers ` ` ` `for` `(ll i = 0; i < n - 1; i++) { ` ` ` `num = (num * 10) + 3; ` ` ` `} ` ` ` `cout << num; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `ll n = 4; ` ` ` ` ` `// Function Call ` ` ` `solve(n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find a ` `// N-digit number such that the number ` `// it is not divisible by its digits ` `class` `GFG { ` ` ` ` ` `long` `ll; ` ` ` ` ` `// Function to find the number ` ` ` `// such that it is not divisible ` ` ` `// by its digits ` ` ` `static` `void` `solve(` `long` `n) ` ` ` `{ ` ` ` `// Base Cases ` ` ` `if` `(n == ` `1` `) ` ` ` `{ ` ` ` `System.out.println(-` `1` `); ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// First Digit of the ` ` ` `// number will be 2 ` ` ` `int` `num = ` `2` `; ` ` ` ` ` `// Next digits of the numbers ` ` ` `for` `(` `long` `i = ` `0` `; i < n - ` `1` `; i++) { ` ` ` `num = (num * ` `10` `) + ` `3` `; ` ` ` `} ` ` ` `System.out.println(num); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `long` `n = ` `4` `; ` ` ` ` ` `// Function Call ` ` ` `solve(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find a ` `# N-digit number such that the number ` `# it is not divisible by its digits ` ` ` `# Function to find the number ` `# such that it is not divisible ` `# by its digits ` `def` `solve(n) : ` ` ` ` ` `# Base Cases ` ` ` `if` `(n ` `=` `=` `1` `) : ` ` ` ` ` `print` `(` `-` `1` `); ` ` ` ` ` `else` `: ` ` ` ` ` `# First Digit of the ` ` ` `# number will be 2 ` ` ` `num ` `=` `2` `; ` ` ` ` ` `# Next digits of the numbers ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `) : ` ` ` `num ` `=` `(num ` `*` `10` `) ` `+` `3` `; ` ` ` ` ` `print` `(num); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `4` `; ` ` ` ` ` `# Function Call ` ` ` `solve(n); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find a ` `// N-digit number such that the number ` `// it is not divisible by its digits ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `long` `ll; ` ` ` ` ` `// Function to find the number ` ` ` `// such that it is not divisible ` ` ` `// by its digits ` ` ` `static` `void` `solve(` `long` `n) ` ` ` `{ ` ` ` `// Base Cases ` ` ` `if` `(n == 1) ` ` ` `{ ` ` ` `Console.WriteLine(-1); ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// First Digit of the ` ` ` `// number will be 2 ` ` ` `int` `num = 2; ` ` ` ` ` `// Next digits of the numbers ` ` ` `for` `(` `long` `i = 0; i < n - 1; i++) { ` ` ` `num = (num * 10) + 3; ` ` ` `} ` ` ` `Console.WriteLine(num); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `long` `n = 4; ` ` ` ` ` `// Function Call ` ` ` `solve(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

2333

**Performance Analysis:**

**Time Complexity:**O(N).**Auxiliary Space:**O(1).

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
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Smallest number with sum of digits as N and divisible by 10^N
- Number of digits to be removed to make a number divisible by 3
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- 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
- Check whether sum of digits at odd places of a number is divisible by K
- Program to check if a number is divisible by sum of its digits
- Program to check if a number is divisible by any of its digits
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B
- 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
- Check whether product of digits at even places of a number is divisible by K
- 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.