
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program for Smallest K digit number divisible by X?
In this problem we will try to find smallest K-digit number, that will be divisible by X. To do this task we will take the smallest K digit number by this formula (10^(k-1)). Then check whether the number is divisible by X or not, if not, we will get the exact number by using this formula.
(min+ 𝑋)−((min+ 𝑋) 𝑚𝑜𝑑 𝑋)
One example is like a 5-digit number, that is divisible by 29. So the smallest 5-digit number is 10000. This is not divisible by 29. Now by applying the formula we will get −
(10000+ 29)−((10000+29) 𝑚𝑜𝑑 29)=10029−24=10005
The number 10005 is divisible by 29.
Algorithm
minKDigit(k, x)
begin min = 10 ^ (k-1) if min is divisible by x, return min otherwise return (min + x) – ((min + x) mod x) end
Example
#include<iostream> #include<cmath> using namespace std; long min_k_digit(int k, int x) { //get the minimum number of k digits int min = pow(10, k-1); if(min % x == 0) { return min; } return (min + x) - ((min + x) % x); } main() { int k, x; cout << "Enter Digit Count(K) and Divisor(N): "; cin >> k >> x; cout << "Result is: " << min_k_digit(k, x); }
Output
Enter Digit Count(K) and Divisor(N): 5 29 Result is: 10005
Output
Enter Digit Count(K) and Divisor(N): 6 87 Result is: 100050
- Related Articles
- Python 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?
- C++ Program for the Largest K digit number divisible by X?
- Java Program for Largest K digit number divisible by X
- Largest K digit number divisible by X in C++
- Find nth number that contains the digit k or divisible by k in C++
- Smallest Integer Divisible by K in Python
- 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