Can somebody explain how it works? I just don't understand it. Why we get 2 from
expr 5 % 3
or 1 from
expr 5 % 4
Can somebody explain how it works? I just don't understand it. Why we get 2 from
expr 5 % 3
or 1 from
expr 5 % 4
x % y gives you the remainder of x/y.
For example, 3 goes into 5 one time. if you subtract 3 from 5, it gives 2. This is what is left over.
In 7 % 3, 3 goes into 7 twice (3*2 < 7, 3*3 > 7). If you subtract 3*2, or 6 from 7, you get one.
7 % 3 == 1
Modulo/modulus is the remainder of the integer division operation. In your first example:
5 % 3
This evaluates to 2
because dividing 5 by 3 leaves 2 remaining.
The modulo operation finds the remainder after division of one number by another.
5 / 3 = 1 remainder 2
5 / 4 = 1 remainder 1