Smallest N digit number divisible by N
Last Updated :
22 Feb, 2023
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>
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>
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Similar Reads
Smallest K digit number divisible by X Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples : Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10Recommended PracticeSmallest K digit number divisible by XTry It! A
4 min read
Smallest n digit number divisible by given three numbers Given x, y, z and n, find smallest n digit number which is divisible by x, y and z. Examples: Input : x = 2, y = 3, z = 5 n = 4Output : 1020Input : x = 3, y = 5, z = 7 n = 2Output : Not possibleRecommended PracticeMighty DivisorTry It!Method: Brute-forceThe brute-force approach to solve this problem
15+ min read
Smallest number divisible by first n numbers Given a number n find the smallest number evenly divisible by each number 1 to n.Examples: Input : n = 4 Output : 12 Explanation : 12 is the smallest numbers divisible by all numbers from 1 to 4 Input : n = 10 Output : 2520 Input : n = 20 Output : 232792560If you observe carefully the ans must be th
8 min read
Count n digit numbers divisible by given number Given number of digit n and a number, the task is to count all the numbers which are divisible by that number and having n digit. Examples : Input : n = 2, number = 7Output : 13Explanation: There are nine n digit numbers that are divisible by 7. Numbers are 14, 21, 28, 35, 42, 49, .... 91, 98 Input
8 min read
Smallest number with sum of digits as N and divisible by 10^N Find the smallest number such that the sum of its digits is N and it is divisible by 10^N . Examples : Input : N = 5 Output : 500000 500000 is the smallest number divisible by 10^5 and sum of digits as 5. Input : N = 20 Output : 29900000000000000000000Recommended PracticeSmallest number with sum of
6 min read
Sum of n digit numbers divisible by a given number Given n and a number, the task is to find the sum of n digit numbers that are divisible by given number.Examples: Input : n = 2, number = 7Output : 728Explanation: There are thirteen n digit numbers that are divisible by 7. Numbers are : 14+ 21 + 28 + 35 + 42 + 49 + 56 + 63 +70 + 77 + 84 + 91 + 98.
9 min read