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
raja
Published on 08-Aug-2019 11:25:02
Advertisements