-4

I never really get it, but what does the modulus operator (%) do.

And when would you want to use it in a mathematical context? By the way, I

just want to know what it does, not how it works.

Samuel L.
  • 123
  • 1
  • 7

1 Answers1

4

It calculates the remainder of a division operation.

5 / 2 == 2   // whole part of the division
5 % 2 == 1   // remainder

In other words you could reconstruct the original number by

2 * 2 + 1 == 5
|   |   ^ remainder
|   ^ whole part
^ divisor
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218