I don't understand the modulo operation. Is the "%" taking a percent or is it just a symbol to indicate modulo? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I don't understand the modulo operation. Is the "%" taking a percent or is it just a symbol to indicate modulo?

31st Dec 2017, 5:57 AM
Jeremy P
Jeremy P - avatar
4 Answers
+ 3
yes 5%2=1 -> 2*2=4 rem=1 4%2=0 -> 2*2=4 rem=0 7%5=2 -> 5*1=5 rem=2
31st Dec 2017, 6:12 AM
‎ ‏‏‎Anonymous Guy
+ 2
It represents the remainder divided by x?
31st Dec 2017, 6:08 AM
Jeremy P
Jeremy P - avatar
0
% is the modulo/modulus operator, which returns the remainder of the division that happens. Think: 5 % 3 as: remainder of 5 / 3 which will lead you to 2. For floor division (// operator), the same applies, but instead of the remainder, you take the whole number part. So 5 // 3 would be 1.
31st Dec 2017, 6:40 AM
LunarCoffee
LunarCoffee - avatar
0
To complement answers, it's very useful for simple and complex things :) For example: If n % 2 == 0, so n is an even number. Or also if you generate a fast random integer number between 1 and n (including), with (randNumber % n) + 1
31st Dec 2017, 6:43 AM
Sebastián Zapata
Sebastián Zapata - avatar