% I dont understand what this arithmetic operator is doing | Sololearn: Learn to code for FREE!


% I dont understand what this arithmetic operator is doing

6/14/2016 4:59:08 PM

Teofil Shterev

3 Answers

New Answer


% modulus operator yields the remainder after division. For example, 1%2 gives 1 or 10%3 gives 1 the remainder or 12%24 givea 12 because it is the remainder and 12 cannot be divided by 24 without a remainder


It's giving the remainder dear!


The modulus operator is very different from division. it does divide, but that number is discarded, whatever is left is printed. in the example of 25%7 we get this; 25/7 does not give a whole number, 21/7 is the closest since we cant increase the starting value of 25. this results in 3, but is discarded. 25-21 is all that matters here, and the answer to that is 4. this can be used to calculate for example; the number of leftover days in a year when you divide the days in a year (365) by the number of weeks in a year (52). 365%52 becomes 364/52 to make the result a whole number. 365-364=1, in this case 1 is printed. Hope this helps:)