+ 2
How useful is the modulo operator?
Could someone explain to me what the modulo could help in terms of making a program and also some examples?
2 Answers
+ 2
the modulo can help in limiting a number to a certain range that you don't mind wrapping around (coordinates for example)
the modulo can help you find out if a number is even or odd
the modulo can be used in encryption (Diffie-Hellman Key Exchange is based on it)
i am sure it's used for lots more!
+ 2
it is also useful for performing operations in loops. for example, index % 2 == 0 would allow you to work on every even number. index % 3 == 0 could be used for every third number ec... Also since integers truncate, it is useful for determining if a value is divisible by another. 8 / 8 is 1 as is 13 / 8 whereras 8 % 8 == 0 so you know that a value is divisible IF it equals 0 after modulo.