Solution for Exercise 11-8

ex1108

#include <stdio.h>

int main()
{
    const int value = 3;
    int a;

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

Output

Modulus 3:
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
8 % 3 = 2
9 % 3 = 0
10 % 3 = 1
11 % 3 = 2
12 % 3 = 0
13 % 3 = 1
14 % 3 = 2
15 % 3 = 0
16 % 3 = 1
17 % 3 = 2
18 % 3 = 0
19 % 3 = 1
20 % 3 = 2
21 % 3 = 0
22 % 3 = 1
23 % 3 = 2
24 % 3 = 0
25 % 3 = 1
26 % 3 = 2
27 % 3 = 0
28 % 3 = 1
29 % 3 = 2

Notes

* The only real change in the code is to Line 5, where the constant value is changed. That has effect beyond Line 5, which is why it's useful to create constants in your code where applicable.

* It's important that you get the order correct when writing a modulo statement: The larger value comes first. When you put the larger value second you won't get the results you want.