Modulo with negative numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Modulo with negative numbers

Can someone explain why these 2 lines in code give different results https://code.sololearn.com/ctGo1AQLJ1Vr/?ref=app

19th Oct 2021, 3:43 AM
Tim
Tim - avatar
1 Answer
+ 1
6%-4 = 2 because 6/-4 = -1 + R/-4 R = 6 - (-1*-4) = 6 - (+4) = 2 -6%4 = -2 because -6/4 = -1 + R/4 R = -6 - (-1*4) = -6 - (-4) = -2
20th Aug 2022, 3:24 AM
Brian
Brian - avatar