# Python Program to Find the Smallest K-Digit Number Divisible by X

In this comprehensive blog post, we’ll delve into a Python program designed to find the smallest K-digit number that is divisible by a given integer X. The program will not only provide the solution but also include detailed explanations of the underlying logic. Readers will gain insights into the problem-solving process, step-by-step code analysis, and how to run the program with sample outputs.

## Problem Statement

Given two integers K and X, the task is to find the smallest K-digit number that is divisible by X.

## Python Program Implementation

Let’s begin by presenting the Python program that tackles this problem:

```
def smallest_k_digit_number(k, x):
# Calculate the minimum and maximum K-digit numbers
min_number = 10 ** (k - 1)
max_number = (10 ** k) - 1
# Find the smallest multiple of X within the range
smallest_multiple = min_number + (x - min_number % x) % x
return smallest_multiple
def main():
# User input
k = int(input("Enter the value of K: "))
x = int(input("Enter the value of X: "))
# Check for valid input
if k <= 0 or x <= 0:
print("Please enter positive values for K and X.")
return
# Find the smallest K-digit number divisible by X
result = smallest_k_digit_number(k, x)
# Display the result
print(f"The smallest {k}-digit number divisible by {x} is: {result}")
if __name__ == "__main__":
main()
```

## Example Usage

Let’s explore how to use the program by running it with sample inputs:

Run the Python program:

```
python smallest_k_digit_number.py
```

Enter the required information when prompted:

```
Enter the value of K: 3
Enter the value of X: 4
```

The program will calculate and display the smallest 3-digit number divisible by 4.

### Example Output

**Example 1:**

```
Enter the value of K: 3
Enter the value of X: 4
The smallest 3-digit number divisible by 4 is: 100
```

**Example 2:**

```
Enter the value of K: 2
Enter the value of X: 7
The smallest 2-digit number divisible by 7 is: 14
```

## Step-by-Step Explanation

**Function**`smallest_k_digit_number()`

:- This function takes two parameters, K and X.
- It calculates the minimum and maximum K-digit numbers using powers of 10.
- Then, it finds the smallest multiple of X within the range from the minimum to maximum K-digit numbers.

**User Input:**- The program uses
`input()`

to obtain user input for the values of K and X.

- The program uses
**Result Display:**- The program prints the smallest K-digit number divisible by X.

**Input Validation:**- The program checks if the entered values for K and X are positive. If not, it prompts the user to enter positive values.

## Logic Behind the Solution

To find the smallest K-digit number divisible by X, the program leverages the properties of divisors and multiples. It calculates the minimum and maximum K-digit numbers using powers of 10 and then iterates through the range to find the smallest multiple of X. The use of modular arithmetic ensures that the result is the smallest K-digit number divisible by X.

## Conclusion

This Python program provides an efficient solution to the problem of finding the smallest K-digit number divisible by a given integer X. Understanding the logic behind the solution enhances problem-solving skills and reinforces mathematical concepts related to divisors and multiples. As you explore this Python program, consider how it optimally calculates the range of K-digit numbers and efficiently identifies the smallest multiple within that range. The program serves as a practical example of combining mathematical principles with programming to solve a real-world problem.