Related Articles

# Smallest N digit number divisible by N

• Last Updated : 27 Jun, 2021

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.

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 Nvoid 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 Codeint main(){    // Given Number    int N = 2;     // Function Call    smallestNumber(N);    return 0;}

## Java

 // Java program for the above approachimport java.util.*;class GFG{ // Function to find the smallest// N-digit number divisible by Nstatic 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 Codepublic 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 Ndef 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 Codeif __name__ == "__main__" :         # Given number    N = 2;     # Function call    smallestNumber(N) # This code is contributed by rock_cool

## C#

 // C# program for the above approachusing System;class GFG{ // Function to find the smallest// N-digit number divisible by Nstatic 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 Codepublic static void Main(){         // Given number    int N = 2;     // Function call    smallestNumber(N);}} // This code is contributed by Nidhi_biet

## Javascript

 
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:
=> => => => 102

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 Nint smallestNumber(int N){     // Return the smallest N-digit    // number calculated using above    // formula    return N * ceil(pow(10, (N - 1)) / N);} // Driver Codeint main(){    // Given N    int N = 2;     // Function Call    cout << smallestNumber(N);    return 0;}

## Java

 // Java program for the above approachimport java.util.*;class GFG{ // Function to find the smallest// N-digit number divisible by Nstatic 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 Codepublic 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 approachimport math # Function to find the smallest# N-digit number divisible by Ndef smallestNumber(N):     # Return the smallest N-digit    # number calculated using above    # formula    return N * math.ceil(pow(10, (N - 1)) // N); # Driver Code # Given NN = 2; # Function Callprint(smallestNumber(N)); # This code is contributed by Code_Mech

## C#

 // C# program for the above approachusing System;class GFG{ // Function to find the smallest// N-digit number divisible by Nstatic 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 Codepublic static void Main(){    // Given N    int N = 2;     // Function Call    Console.Write(smallestNumber(N));}} // This code is contributed by Code_Mech

## Javascript

 
Output:
10

Time Complexity: O(1)
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.  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