+ 4

Is there anybody who help me with this code ?

How the output of this code is 4? https://code.sololearn.com/ceDTayd8dYcn/?ref=app

17th Aug 2019, 5:06 PM
Shahid Iqbal
Shahid Iqbal - avatar
15 Respostas
+ 11
The modulus operator calculates the remainder of a division. 9 % 5 % 5 from left to right: 9 % 5 = 4 9 / 5 = 1 -> 9 - 5 = 4 4 % 5 = 4 4 / 5 = 0 -> 4 - 0 = 4
17th Aug 2019, 5:14 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
17th Aug 2019, 5:09 PM
Shahid Iqbal
Shahid Iqbal - avatar
17th Aug 2019, 5:09 PM
Shahid Iqbal
Shahid Iqbal - avatar
+ 2
First you want see operator precedence Only modulo operator used in this code So we process left to right 9%5%5 9%5=4 4%5=4 Ans=4
17th Aug 2019, 5:49 PM
Praveenkumar
Praveenkumar - avatar
+ 2
9%5=4 because 9 devided by 5 would be 1 4/5, or 1.8. The % operator gives you the left over that couldn't be devided and removes the devided amount. Therefore 9%5=4. Other examples might be: 20%5=0. 21%5=1. 21%4=1. 12%4=0. I hope this helps.
18th Aug 2019, 6:46 AM
SoCal
SoCal - avatar
+ 2
Numerator less than denominator so we take numerator in modulo operator
20th Aug 2019, 2:44 AM
Praveenkumar
Praveenkumar - avatar
+ 1
17th Aug 2019, 7:55 PM
Shahid Iqbal
Shahid Iqbal - avatar
+ 1
Shahid Iqbal If you calc 4 / 5 you get 0. There is still a rest of 4, so 4 % 5 = 4. Another example: 3 / 9 = 0 rest 3 -> 3 % 9 = 3. 6 / 8 = 0 rest 6 -> 6 % 8 = 6
17th Aug 2019, 8:07 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Denise Roßberg got it man ❀❀
17th Aug 2019, 8:16 PM
Shahid Iqbal
Shahid Iqbal - avatar
+ 1
I think you understood how modulo (remainder) operator works in java. But if you want to know more, you can read this: https://www.java67.com/2014/11/modulo-or-remainder-operator-in-java.html?m=1
17th Aug 2019, 8:51 PM
Mykola Romaniak
Mykola Romaniak - avatar
+ 1
Thank you bro :)
18th Aug 2019, 5:17 AM
Shahid Iqbal
Shahid Iqbal - avatar
+ 1
This is because 4 can't be devided by 5. So there is 4 "left over" from the act of the devision.
18th Aug 2019, 6:42 AM
SoCal
SoCal - avatar
+ 1
SoCal ❀
18th Aug 2019, 6:48 AM
Shahid Iqbal
Shahid Iqbal - avatar
0
Still confused :(
17th Aug 2019, 7:57 PM
Shahid Iqbal
Shahid Iqbal - avatar
0
Praveenkumar gotcha :)
20th Aug 2019, 9:30 AM
Shahid Iqbal
Shahid Iqbal - avatar