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.


#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 + X) - ((MIN + X) % X));
   cout << answer(X, K);


The smallest 5 digit number divisible by 83 is 100430