+ 2

[Solved] I can't understand this.

What is %? Example int a = 10 % 5; Or /= (+=, - =)

4th Jan 2017, 11:24 PM
FinKsu
2 Respostas
+ 5
a /= b is the same as a = a/b, same for += and -= % is the modulus operator, it's basically the leftover of integer division: 20/6 = 3 with 2 left (20 = 3*6 + 2), so 20%6 is 2. 19%6 = 1 (19 = 3*6 + 1) 18%6 = 0 (18 = 3*6 + 0) 17%6 = 5 (17 = 2*6 + 5) So your example 10%5 = 0 The best use for % is probably to see if a number is odd or even: a%2 will return 0 if a is even and 1 if it's odd.
4th Jan 2017, 11:52 PM
Robobrine
Robobrine - avatar
+ 3
Thank you Robobrine!
4th Jan 2017, 11:37 PM
FinKsu