Everything follows from the fundamental rules of integer division. For integers x
and y
suppose we have:
q = x / y;
r = x % y;
The fundamental rules are:
q
and r
are both integers
q * y + r == x
- Division rounds towards zero when inexact
Now that you know the fundamental rules you can work out the values of 5 / 8
and 5 % 8
.
5 / 8
is an integer. The exact value is 0.625
, but that's not an integer, so we round towards zero and get 0
. So q
is 0
.
To compute r we must solve for q * 8 + r = 5
, which is easy to solve: r = 5
is the solution.
(Note that there are a few more fundamental rules about dividing by zero, and so on, that you usually don't have to worry about.)