Using the % remainder operator

I am trying to understand the % remainder operator. In the reference section there is some example code as in:

int x = 0;
x = 7 % 5;  // x now contains 2
x = 9 % 5;  // x now contains 4
x = 5 % 5;  // x now contains 0
x = 4 % 5;  // x now contains 4
x = -4 % 5; // x now contains -4
x = 4 % -5; // x now contains 4

Where remainder = dividend % divisor; and Remainder operation calculates the remainder when one integer is divided by another.

How are these numbers derived? The quotient of 7/5 is 1.4 So wouldn't the remainder be 0.4, not 2? Similar with the rest: 9/5 is 1.8 so the remainder is 0.8 not 4 as shown above. Doesn't seem to be rounding up. What am I not understanding?

7 / 5 = 1 with 2 left over

7 % 5 = 2

120 % 100 = 20

I get it now. Thanks LarryD!

% is MODULUS. Also known as BASE. We count in BASE 10 or MODULUS 10.

A base 5 counting system counts 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20...

So, a 9 (base 10) Modulus 5 is 14 or 1 remainder 4 (14 being the "number" preceding 20 base 5).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.