1. ## Modulus Operator

Hi all,

I'm looking at the modulus operator and having a hard time understanding its practical purpose. The book shows me:

Code:
```#include <stdio.h>
{
int a = 25, b = 5, c = 10, d = 7;

printf ("a %% b = %i\n, a % b);
printf ("a %% c = %i\n, a % c);
printf ("a %% d = %i\n", a % d);
printf ("a/d*d+a %% d = %i\n", a /d*d+a % d);

return 0;
}```
While I understand what is happening here, I have a hard time invisioning other practical programming situations where this operator is useful. Can any one provide practical examples, and use common language to better explain why the modulus operator is useful?

2. It's used a lot in cryptography, because it allows you to perform a math operation and have a fixed-size result. For instance, you may need to multiply 32-bit numbers, and end up with a 32 bit-result. In regular arithmetic, you could use as much as 64 bits, but performing the operation mod 2^32 ensures it will fit in with the rest of the system. That's a very difficult operation to reverse, but a very easy one to verify - which makes it useful for security (see discrete logarithms, etc...). This is also sometimes accomplished using the size of the data type. If you multiply 2 32-bit integers together and try to store the result in another 32-bit integer, the result is automatically mod 2^32, without having to perform any additional math.

It also provides circular arithmetic. If you have a schedule that rotates between 4 people, it's easy to know who's turn it is on a given day if you take the day number mod 4.

Say you want a random number between 1 and 10. The C standard library has a function to provide random numbers, but the range is pretty much any integer. If you take any integer mod 10 plus one, you end up with a number between 1 and 10. (For the sake of correctness, I should point out that you end up with a slight bias, since 10 doesn't fit evenly into the range of an integer - but you get the idea).

There are many others - but that should give you a taste. Rest assured - it is very useful!

3. Another example is when converting an integer (short, int, or long) to a string such as is done when implementing some of the conversions in fprintf() or snprintf() or related functions.

4. I knew I could find the answers I was looking for here, Thanks all :-)

5. This statement...

Code:
`int x = (14 % 6);`
...assigns x to the remainder of fourteen divided by six. Therefore, x == 2.

6. As a side note, I'd be careful using the term "remainder". Some languages have both a 'remainder' and a 'modulus' operator, and they behave slightly differently when it comes to negative numbers. It's important to know the difference and what the language being used uses. Wikipedia has a good table of the operators and their behavior.

Modulo operation - Wikipedia, the free encyclopedia

7. It's also commonly used in formatting output with a lot of data when printed with iterations.

If you needed to display, say 10,000 numbers, % c could mean to place a newline every c numbers, meaning to list c columns of numbers every line.