0
how does the modulus work? still dont get it
6 Réponses
+ 1
let's first know what's division here. it will clear the doubt about modulus operation.
take an example: 5/3 . here 5 --> dividend, 3 --> divisor and / --> division operator.
as we know when we devide one number by another one result will have two entities. one is 'quotient' and the other one is 'remainder'.
when we devide 5 by 3 , it yields 1 as 'quotient' and 2 as 'remainder'.
In computer programming languages such as c, c++ and Java etc. / operator used to output quotient. and % operator used to output remainder.
Hence, 5/3=1 and 5%3=2.
+ 1
See my "% Modulus explained" code to understand in a way, you may have learnt at school
https://code.sololearn.com/c2fRl6x893E7/?ref=app
0
Here's a quick rundown of how modulus works. Take the following as the equation your using:
11 % 2;
Here, we are basically doing do 11 / 2 however, 2 cannot go into 11 perfectly.
So, we divide 11 as much as we can without having a fraction or decimal in the end.
So, 2 can go into 11 five times.
2 * 5 = 10
Now you take 11 and subtract it by 10:
11 - 10 = 1
Here you have your remainder.
Now, modulus simplifies this in that it crams all of this work into one sign.
TL;DR Version:
Modulus shortens the work needed to get the remainder from integer division.
I hope this helps!
0
thank u so much!
0
No problem!
0
thanks