How useful is the modulo operator? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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?

21st Nov 2016, 4:34 AM
James Nguyen
James Nguyen - avatar
2 Réponses
+ 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!
21st Nov 2016, 4:38 AM
asdadasdsaczxc
+ 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.
22nd Nov 2016, 3:15 AM
Andrew Michel
Andrew Michel - avatar