would somebody like to explain how modulo(%) operator works -7%3 = 1, 7%-3 = -2 , negative numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

would somebody like to explain how modulo(%) operator works -7%3 = 1, 7%-3 = -2 , negative numbers?

Please give intuitive explanation and not algorithm to calculate it (I am weak at math )

3rd May 2019, 12:10 AM
ishwar
ishwar - avatar
5 Answers
+ 4
Thanks a lot
3rd May 2019, 1:07 AM
ishwar
ishwar - avatar
+ 3
% gives you the remainder from division. So 7%2 will return 1 because 2 goes into 7, 3 times (6) with 1 left over.
3rd May 2019, 12:13 AM
Ryan Lusby
Ryan Lusby - avatar
+ 3
Some differences exist between simply getting a remainder of a division and the actual modulus. Can't really come up with an "intuitive" explanation (not to say there isn't one, but) - You want to understand how it works that way, that directly translates to "the algorithm behind it", and yet you don't want algorithms, so I'm not sure how to approach this. This thread should give a good overview of the topic though. https://www.sololearn.com/Discuss/989220/?ref=app
3rd May 2019, 12:27 AM
Fermi
Fermi - avatar
+ 3
In computing, the modulo operation finds the remainder after division of one number by another (sometimes called modulus). Given two positive numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n. For example, the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a quotient of 3 and leaves a remainder of 0; there is nothing to subtract from 9 after multiplying 3 times 3. (Doing the division with a calculator will not show the result referred to here by this operation; the quotient will be expressed as a decimal fraction.) In this example, -7 / 3 => 3 × 2=6, 7-6 = 1 but -7 + 6 = -1.
17th May 2019, 11:44 AM
Akhil Hothi
Akhil Hothi - avatar
+ 1
Kindly read post properly Thanks for answering anyway
3rd May 2019, 12:15 AM
ishwar
ishwar - avatar