# Smallest N digit number divisible by N

Given a positive integers N, the task is to find the smallest N digit number divisible by N.

Examples:

Input: N = 2
Output: 10
Explanation:
10 is the smallest 2-digit number which is divisible by 2.

Input: N = 3
Output: 102
Explanation:
102 is the smallest 3-digit number which is divisible by 3.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The naive approach is to iterate from smallest N-digit number(say S) to largest N-digit number(say L). The first number between [S, L] divisible by N is the required result.

Below is the implementation of above approach:

## C++

 // C++ program for the above approach  #include  #include  using namespace std;     // Function to find the smallest  // N-digit number divisible by N  void smallestNumber(int N)  {      // Find largest n digit number      int L = pow(10, N) - 1;         // Find smallest n digit number      int S = pow(10, N - 1);         for (int i = S; i <= L; i++) {             // If i is divisible by N,          // then print i and return ;          if (i % N == 0) {                 cout << i;              return;          }      }  }     // Driver Code  int main()  {      // Given Number      int N = 2;         // Function Call      smallestNumber(N);      return 0;  }

## Java

 // Java program for the above approach  import java.util.*;  class GFG{     // Function to find the smallest  // N-digit number divisible by N  static void smallestNumber(int N)  {         // Find largest n digit number      int L = (int) (Math.pow(10, N) - 1);         // Find smallest n digit number      int S = (int) Math.pow(10, N - 1);         for (int i = S; i <= L; i++)       {             // If i is divisible by N,          // then print i and return ;          if (i % N == 0)           {              System.out.print(i);              return;          }      }  }     // Driver Code  public static void main(String[] args)  {      // Given Number      int N = 2;         // Function Call      smallestNumber(N);  }  }     // This code is contributed by Amit Katiyar

## Python3

 # Python3 program for the above approach     # Function to find the smallest  # N-digit number divisible by N  def smallestNumber(N):         # Find largest n digit number      L = pow(10, N) - 1;         # Find smallest n digit number      S = pow(10, N - 1);         for i in range(S, L):              # If i is divisible by N,          # then print i and return ;          if (i % N == 0):               print(i);              return;             # Driver Code   if __name__ == "__main__" :             # Given number      N = 2;         # Function call      smallestNumber(N)     # This code is contributed by rock_cool

## C#

 // C# program for the above approach  using System;  class GFG{     // Function to find the smallest  // N-digit number divisible by N  static void smallestNumber(int N)  {         // Find largest n digit number      int L = (int)(Math.Pow(10, N) - 1);         // Find smallest n digit number      int S = (int)Math.Pow(10, N - 1);         for(int i = S; i <= L; i++)       {                   // If i is divisible by N,         // then print i and return ;         if (i % N == 0)          {             Console.Write(i);             return;         }      }  }     // Driver Code  public static void Main()  {             // Given number      int N = 2;         // Function call      smallestNumber(N);  }  }     // This code is contributed by Nidhi_biet

Output:

10


Time Complexity: O(L – S), where L and S is the largest and smallest N-digit number respectively.

Efficient Approach: If the number divisible by N, then the number will be of the form N * X for some positive integer X.
Since it has to be smallest N-digit number, then X will be given by: .
Therefore, the smallest number N-digit number is given by:

For Example:

For N = 3, the smallest 3-digit number is given by:
=>
=>
=>
=>

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach  #include  #include  using namespace std;     // Function to find the smallest  // N-digit number divisible by N  int smallestNumber(int N)  {         // Return the smallest N-digit      // number calculated using above      // formula      return N * ceil(pow(10, (N - 1)) / N);  }     // Driver Code  int main()  {      // Given N      int N = 2;         // Function Call      cout << smallestNumber(N);      return 0;  }

## Java

 // Java program for the above approach  import java.util.*;  class GFG{     // Function to find the smallest  // N-digit number divisible by N  static int smallestNumber(int N)  {         // Return the smallest N-digit      // number calculated using above      // formula      return (int) (N * Math.ceil(Math.pow(10, (N - 1)) / N));  }     // Driver Code  public static void main(String[] args)  {      // Given N      int N = 2;         // Function Call      System.out.print(smallestNumber(N));  }  }     // This code is contributed by Princi Singh

## Python3

 # Python3 program for the above approach  import math     # Function to find the smallest  # N-digit number divisible by N  def smallestNumber(N):         # Return the smallest N-digit      # number calculated using above      # formula      return N * math.ceil(pow(10, (N - 1)) // N);     # Driver Code     # Given N  N = 2;     # Function Call  print(smallestNumber(N));     # This code is contributed by Code_Mech

## C#

 // C# program for the above approach  using System;  class GFG{     // Function to find the smallest  // N-digit number divisible by N  static int smallestNumber(int N)  {         // Return the smallest N-digit      // number calculated using above      // formula      return (int) (N * Math.Ceiling(Math.Pow(10, (N - 1)) / N));  }     // Driver Code  public static void Main()  {      // Given N      int N = 2;         // Function Call      Console.Write(smallestNumber(N));  }  }     // This code is contributed by Code_Mech

Output:

10


Time Complexity: 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.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

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