+ 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
15 Answers
+ 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
+ 2
Mykola Romaniak bro :)
+ 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
+ 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.
+ 2
Numerator less than denominator so we take numerator in modulo operator
+ 1
4%5=4??? How Denise Roßberg and Praveen[Java]
+ 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
+ 1
Denise Roßberg got it man ❤❤
+ 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
+ 1
Thank you bro :)
+ 1
This is because 4 can't be devided by 5. So there is 4 "left over" from the act of the devision.
+ 1
SoCal ❤
0
Still confused :(
0
Praveenkumar gotcha :)