It's pretty easy to figure out if you know modular arithmetics, expression (b[n] + 10 * b[n - 1] + ... + 10^k * b[k] + ... + 10^n * b[0]) modulo a
which is technically initial problem statement could be simplified to (...((b[0] modulo a) * 10 + b[1]) modulo a) * 10 + ... + b[n]) modulo a
which is what your algorithm does.
To prove that their equal we may calculate coefficient modulo a
before b[i]
in the second expression, it's easy to see that for b[i]
there will be exactly n - i
times we'll have to multiply it by 10 (the last one which is n
will be multiplied 0 times, the one before him 1 time and so on ...). So modulo a
it equals 10 ^ (n - i)
which is the same coefficient before b[i]
in the first expression.
Thus since all coefficients before b[i]
in both expressions would be equal, it's obvious that both expressions are equal to (k_0 * b[0] + k_1 * b[1] ... + k_n * b[n])
modulo a
and thus they are equal modulo a
.
48
is char code for 0
digit, so (b[i] - 48)
is conversion from char to digit.