7 % 3 = 1 (remainder 1)
how does
3 % 7
(remainder ?)
work?
7 % 3 = 1 (remainder 1)
how does
3 % 7
(remainder ?)
work?
remainder of 3/7 is 3..since it went 0 times with 3 remainder so 3%7 = 3
The same way. The quotient is 0 (3 / 7 with fractional part discarded). The remainder then satisfies:
(a / b) * b + (a % b) = a
(3 / 7) * 7 + (3 % 7) = 3
0 * 7 + (3 % 7) = 3
(3 % 7) = 3
This is defined in C99 §6.5.5, Multiplicative operators.
7 goes into 3? zero times with 3 left over.
quotient is zero. Remainder (modulus) is 3.
a % q = r means there is a x so that q * x + r = a.
So, 7 % 3 = 1 because 3 * 2 + 1 = 7,
and 3 % 7 = 3 because 7 * 0 + 3 = 3
As long as they're both positive, the remainder will be equal to the dividend. If one or both is negative, then you get reminded that %
is really the remainder operator, not the modulus operator. A modulus will always be positive, but a remainder can be negative.
7 divided by 3 is 2 with a remainder of 1
3 divided by 7 is 0 with a remainder of 3