C++ Programming for Smallest K digit number divisible by X?



Smallest K digit number that divisible by X is found using the formula by checking divisible by X. The formula works in the following way −

Compute minimum K digit number [min] for example: 10/100/1000 etc.

Now find if min is divisible by X. if yes, then this is the answer.

If not, then min+X - ([min+X]%k) is the answer.

Example

#include <iostream> #include <math.h> using namespace std; int main() {    int X = 83;    int K = 5;    cout<<"The smallest "<<K<<" digit number divisible by "<<X<<" is ";    int MIN = pow(10, K - 1);    if (MIN % X == 0)       cout<<MIN;    cout<<((MIN + X) - ((MIN + X) % X));    cout << answer(X, K); }

Output

The smallest 5 digit number divisible by 83 is 100430
Updated on: 2019-08-08T07:55:02+05:30

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements