
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program for Smallest K digit number divisible by X
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Integers n and d are given. We need to find the smallest n-digit number divisible by d.
Approach
1. FirstNow let's we compute MIN : smallest n-digit number (1000...n-times)
2. Now, If MIN % X is 0, ans = MIN
3. else, ans = (MIN + X) - ((MIN + X) % X))
This is because there will be a number in range [MIN...MIN+X] which is divisible by d.
Now let’s see the implementation −
Example
def answer(n, d): # Computing MAX Min = pow(10, d-1) if(Min%n == 0): return (Min) else: return ((Min + n) - ((Min + n) % n)) n = 83 d = 5 print(answer(n, d))
Output
10043
All the variables are declared in the global frame as shown in the figure given below −
Conclusion
In this article, we learnt about the approach to find Smallest K digit number divisible by X
- Related Articles
- C++ Program for Smallest K digit number divisible by X?
- Java Program for Smallest K digit number divisible by X
- C++ Programming for Smallest K digit number divisible by X?
- C++ Program for Largest K digit number divisible by X?
- Java Program for Largest K digit number divisible by X
- C++ Program for the Largest K digit number divisible by X?
- Largest K digit number divisible by X in C++
- Smallest Integer Divisible by K in Python
- Find nth number that contains the digit k or divisible by k in C++
- Which is the smallest 4-digit number divisible by 8, 10 and 12?
- Find the smallest 5-digit number which is divisible by 12, 18, 30.
- Find the smallest 4-digit number which is divisible by 18,24 and 32 .
- Find the smallest 5 digit number which is exactly divisible by 20,25 and 30.
- Determine the smallest 3-digit number which is exactly divisible by 6,8 and 12.
- Find the smallest 4 digit number which is exactly divisible by 18, 24, 36.

Advertisements