Both answers are correct since -1 modulo 10
is the same as 9 modulo 10
.
r = (a mod m)
a = n*q + r
You can be sure that |r| < |n|
, but not what the value of r
is. There are 2 answers, negative and positive.
In C89, although the answer will always be correct, the exact value of a modulo operation (they refer to it as remainder) is undefined, meaning it can be either a negative result or a positive result. In C99 the result is defined.
If you want the positive answer though, you can simply add 10 if you find your answer is negative.
To get the modulo operator to work the same on all languages, just remember that:
n mod M == (n + M) mod M
and in general:
n mod M == (n + X * M) mod M