0

What does a%b mean?

Hi guys, I am very knew to java and this question is often asked in challenges, I was wondering what does the following mean, I believe I missed it in tutorials: Class question { public static void main (String [] args) { int a=10; int b=1; double variable =(a%b); int variable2=(a%b); System.out.println(variable); System.out.println(variable2); //just in case } } // output is 0. 0 0 So what does % mean?

25th Dec 2016, 2:19 PM
Andrej Maister
Andrej Maister - avatar
4 Answers
+ 5
It will be like what @James stated. To understand that : 5 % 2 == 1 5 divided by 2 would be 2.5 , if you ignore the 0.5 and divide 5 by 2 two times only, the '5' would have a '1' that is left undivided, hence the remainder is 1. 8 % 4 == 0 4 can divide a 8 perfectly 2 times, hence there will be nothing left undivided. 32 % 10 == 2 32 divided by 10 would give 3.2. If you ignore the 0.2 and divided 32 by 10 three times, there will be left a '2' that is undivided. Hence, the remainder is '2'
25th Dec 2016, 2:41 PM
Wen Qin
Wen Qin - avatar
+ 4
That is the modulo operator. You can think of it as the same as doing division but instead of getting the quotient you get the remainder out. Here are some examples: 5 % 2 == 1 8 % 4 == 0 32 % 10 == 2
25th Dec 2016, 2:24 PM
James Durand
James Durand - avatar
+ 4
It gives you the remainder of the int division, for example 9%3=0 because nine divided by three gives you 3 without any decimals 9%5= 4 because 5 enters only one time and as we are working with intergers we don't have decimals, so the remainder is 4
25th Dec 2016, 2:42 PM
C.E.
C.E. - avatar
+ 1
Thanks guys it's clear! :)
25th Dec 2016, 2:43 PM
Andrej Maister
Andrej Maister - avatar