How Does Modulus Divison Work
Solution 1
The result of a modulo division is the remainder of an integer division of the given numbers.
That means:
27 / 16 = 1, remainder 11
=> 27 mod 16 = 11
Other examples:
30 / 3 = 10, remainder 0
=> 30 mod 3 = 0
35 / 3 = 11, remainder 2
=> 35 mod 3 = 2
Solution 2
Most explanations miss one important step, let's fill the gap using another example.
Given the following:
Dividend: 16
Divisor: 6
The modulus function looks like this:
16 % 6 = 4
Let's determine why this is.
First, perform integer division, which is similar to normal division, except any fractional number (a.k.a. remainder) is discarded:
16 / 6 = 2
Then, multiply the result of the above division (2
) with our divisor (6
):
2 * 6 = 12
Finally, subtract the result of the above multiplication (12
) from our dividend (16
):
16 - 12 = 4
The result of this subtraction, 4
, the remainder, is the same result of our modulus above!
Solution 3
The simple formula for calculating modulus is :-
[Dividend-{(Dividend/Divisor)*Divisor}]
So, 27 % 16 :-
27- {(27/16)*16}
27-{1*16}
Answer= 11
Note:
All calculations are with integers. In case of a decimal quotient, the part after the decimal is to be ignored/truncated.
eg: 27/16= 1.6875 is to be taken as just 1 in the above mentioned formula. 0.6875 is ignored.
Compilers of computer languages treat an integer with decimal part the same way (by truncating after the decimal) as well
Solution 4
Maybe the example with an clock could help you understand the modulo.
A familiar use of modular arithmetic is its use in the 12-hour clock, in which the day is divided into two 12 hour periods.
Lets say we have currently this time: 15:00
But you could also say it is 3 pm
This is exactly what modulo does:
15 / 12 = 1, remainder 3
You find this example better explained on wikipedia: Wikipedia Modulo Article
Solution 5
The modulus operator takes a division statement and returns whatever is left over from that calculation, the "remaining" data, so to speak, such as 13 / 5 = 2. Which means, there is 3 left over, or remaining from that calculation. Why? because 2 * 5 = 10. Thus, 13 - 10 = 3.
The modulus operator does all that calculation for you, 13 % 5 = 3.
Related videos on Youtube
NSWOA
Updated on July 08, 2022Comments
-
NSWOA almost 2 years
I don't really understand how modulus division works. I was calculating
27 % 16
and wound up with11
and I don't understand why.I can't seem to find an explanation in layman's terms online. Can someone elaborate on a very high level as to what's going on here?
-
Code Maverick about 10 yearsCould you please format this answer a little better?
-
JonnyB about 9 yearsI think this answer explains it the best from a conceptual standpoint. Other answers explain mathematically which is also necessary, but this better helps me understand how I could apply the modulo operator.
-
Soundfx4 about 9 yearsplease don't take this the wrong way, but your examples do not clear anything up for someone that has absolutely no clue what's going on with modulous divison. You left out very important steps that explain where that remainder comes from. Marcin M.'s answer below explained the process better. Please consider being more detailed in future answers for those of us that may not have a grasp on a concept at all. Thank you for being a contributing member to the community though! People like you help me out, and continue to help me out on my educational journey :)
-
Luc almost 8 yearsHow do you get 2 out of 16 / 6 and not 2,6666666667? Should you always just ignore the 0,...? Why?
-
bryik over 7 years@Luc As Leo and ytpillai mention, we're using integer division (where the fractional part of the result after dividing is discarded). In Python 3:
16 // 6 >>> 2
and16 / 6 >>> 2.6666666666666665
-
Ajmal Salim almost 7 yearsCheck Code Jammer's answer.
-
eaglei22 over 6 yearsWhat about 3 % 7 ?
-
eaglei22 over 6 yearsSo it would just be 3?
-
user207421 over 5 yearsWikipedia notwithstanding, modulus and remainder are not the same thing. Some languages have one, some the other, some both, and some undefined.
-
Bob Jordan almost 5 yearsSurprisingly useful with regards to pagination.