Solution for Exercise 11-7

ex1107

#include <stdio.h>

#define VALUE 5

int main()
{
    int a;

    printf("Modulus %d:\n",VALUE);
    for(a=0;a<30;a++)
        printf("%d %% %d = %d\n",a,VALUE,a%VALUE);
    return(0);
}

Notes

* Here is a sample of the program's output:

* Modulus is also known as the "remainder operator." When you take the modulus of two values, the result is the remainder for the first divided by the second. You can see that result above. For example, 8 divided by 5 leaves a remainder of 3.

* When the modulus operation divides evenly, the value zero is returned.

* A great way to use modulus is when your code needs to count every nth item in a list. That expression looks like this in C:

For example, the value %2 might be used to highlight every other item in a list.