Explain me why -4%3 output -1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Explain me why -4%3 output -1

7th Jan 2021, 9:17 AM
Elon
Elon - avatar
8 Answers
+ 5
% -->modulus operator(remainder) 4%3 => Remainder on dividing 4 by 3 which is 1 . Therefore -4%3 = -1
7th Jan 2021, 9:20 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 4
Alphin K Sajan is it a convention that the result take sign of first number or it can be demonstrated?
7th Jan 2021, 9:23 AM
Elon
Elon - avatar
+ 4
Elon Yemp Yes,When we use modulus operator, the first value 's sign is considered, but not the second one's. Eg: -4 % 3= -1 -4 % -3= -1 But, 4 % -3 = 1 Hope now u understood !!
7th Jan 2021, 9:34 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
John Robotane but with JavaScript (-4)%3 output -1
7th Jan 2021, 9:46 AM
Elon
Elon - avatar
+ 3
John Robotane javascript output the same result for -(4%3) -4%3 and (-4%3)
7th Jan 2021, 9:52 AM
Elon
Elon - avatar
+ 1
this may help: (-4)%3=2 -4%3=-(4%3)=-(1)=-1
7th Jan 2021, 9:35 AM
John Robotane
John Robotane - avatar
+ 1
Elon Yemp Wlcm :)
7th Jan 2021, 9:40 AM
Alphin K Sajan
Alphin K Sajan - avatar
0
Alphin K Sajan yes i understand now! Thanks
7th Jan 2021, 9:38 AM
Elon
Elon - avatar