Open In App

Smallest N digit number divisible by N

Last Updated : 22 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 <iostream>
#include <math.h>
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
JavaScript
<script>

// Javascript program for the above approach

// Function to find the smallest
// N-digit number divisible by N
function smallestNumber(N)
{
    
    // Find largest n digit number
    let L = Math.pow(10, N) - 1;

    // Find smallest n digit number
    let S = Math.pow(10, N - 1);

    for(let i = S; i <= L; i++)
    {
        
        // If i is divisible by N,
        // then print i and return ;
        if (i % N == 0)
        {
            document.write(i);
            return;
        }
    }
}

// Driver code

// Given Number
let N = 2;

// Function Call
smallestNumber(N);
    
// This code is contributed by divyeshrabadiya07

</script>

Output: 
10

 

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

Auxiliary Space: O(1)

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: 

\lceil \frac{10^{N-1}}{N} \rceil       . Therefore, the smallest number N-digit number is given by: 

N*\lceil \frac{10^{N-1}}{N} \rceil    


For Example:  

For N = 3, the smallest 3-digit number is given by: 
=> 3*\lceil \frac{10^{3-1}}{3} \rceil    

=> 3*\lceil \frac{100}{3} \rceil    

=> 3*\lceil 33.3 \rceil    

=> 102

Below is the implementation of the above approach: 

C++
// C++ program for the above approach
#include <iostream>
#include <math.h>
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
JavaScript
<script>
    // Javascript program for the above approach
    
    // Function to find the smallest
    // N-digit number divisible by N
    function smallestNumber(N)
    {

        // Return the smallest N-digit
        // number calculated using above
        // formula
        return N * Math.ceil(Math.pow(10, (N - 1)) / N);
    }
    
    // Given N
    let N = 2;
 
    // Function Call
    document.write(smallestNumber(N));
    
    // This code is contributed by divyesh072019.
</script>

Output: 
10

 

Time Complexity: O(log(N))
Auxiliary Space: O(1)


Similar Reads