Smallest N digit number divisible by N

By | November 30, 2024

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

Examples:

Input: N = 4
Output: 1000
Explanation:
1000 is the smallest 4-digit number divisible by 4.

Input: N = 5
Output: 10000
Explanation:
10000 is the smallest 5-digit number divisible by 5.

Approach:

  • A series will be formed like 1, 10, 102, 1000, 10000, 100002, 1000006, 10000000……… for n = 1, 2, 3…..
  • So the Nth term will be = n*\lceil 10^\frac{n-1}{n} \rceil

Below is the implementation of the above approach

// C++ program to find the smallest 
// K digit number divisible by K.
#include <iostream>
#include <cmath>
using namespace std;

// Function to find the smallest 
// K digit number divisible by K.
void smallestDivisibleNumber(int K)
{  
    cout << K * ceil(pow(10, (K - 1)) / K);
}

// Driver code
int main()
{
    int K = 4;  
    smallestDivisibleNumber(K);
    
    return 0;
}

Output:

1000
Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.