Use of Modulus

Modulus is more useful than you think

All programming languages have a mathematical function called modulus. Normally the symbol is %
Modulus just means find the remainder. 11 Mod 5 would result in 1 because 11/5 = 2 remainder 1
What happens if the first number is smaller? e.g. 5 Mod 11? The result is 5 because 5 / 11 is 0 remainder 5.

Modulus is used in at least four ways:

  1. to find the remainder of something
    remainder = 13 % 3
  2. to see if a number is a multiple of another number
    if (x % 2 == 0)  // the number is even 
  3. to limit the size of a number
    e.g. n%256 will make sure that the expression never goes above 255. It goes from 0-255 and then repeats.
     
  4. to do something after a certain number of repetitions like adding a form-feed every 40 lines
    if (linecount % 40 == 0) print_form_feed(); 

There are three programs to do:

Modulus1.java

Write a program that will print out the numbers 0-100 in the first column along with the numbers 0-7 cycling in the second column. Use modulus to get the second column.
It should look like this:
0	0
1	1
2	2
3	3
4	4
5	5
6	6
7	7
8	0
9	1
10	2
11	3
12	4
13	5
14	6
15	7
16	0
17	1
...

Modulus2.java

Write a program that uses modulus to print out only the multiples of 13 between 0 and 200.
13 26 39 52 65 78 91 104 117 130 143 156 169 182 195

Modulus3.java

Write a program that prints out the numbers from 1 to 200. Use modulus to insert a newline character after every multiple of 12.
It should look like this:

1 2 3 4 5 6 7 8 9 10 11 12 
13 14 15 16 17 18 19 20 21 22 23 24 
25 26 27 28 29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 46 47 48 
49 50 51 52 53 54 55 56 57 58 59 60 
61 62 63 64 65 66 67 68 69 70 71 72 
73 74 75 76 77 78 79 80 81 82 83 84 
85 86 87 88 89 90 91 92 93 94 95 96 
97 98 99 100 101 102 103 104 105 106 107 108 
109 110 111 112 113 114 115 116 117 118 119 120 
121 122 123 124 125 126 127 128 129 130 131 132 
133 134 135 136 137 138 139 140 141 142 143 144 
145 146 147 148 149 150 151 152 153 154 155 156 
157 158 159 160 161 162 163 164 165 166 167 168 
169 170 171 172 173 174 175 176 177 178 179 180 
181 182 183 184 185 186 187 188 189 190 191 192 
193 194 195 196 197 198 199

Now use printf to line up all of the numbers.

  1   2   3   4   5   6   7   8   9  10  11  12 
 13  14  15  16  17  18  19  20  21  22  23  24 
 25  26  27  28  29  30  31  32  33  34  35  36 
 37  38  39  40  41  42  43  44  45  46  47  48 
 49  50  51  52  53  54  55  56  57  58  59  60 
 61  62  63  64  65  66  67  68  69  70  71  72 
 73  74  75  76  77  78  79  80  81  82  83  84 
 85  86  87  88  89  90  91  92  93  94  95  96 
 97  98  99 100 101 102 103 104 105 106 107 108 
109 110 111 112 113 114 115 116 117 118 119 120 
121 122 123 124 125 126 127 128 129 130 131 132 
133 134 135 136 137 138 139 140 141 142 143 144 
145 146 147 148 149 150 151 152 153 154 155 156 
157 158 159 160 161 162 163 164 165 166 167 168 
169 170 171 172 173 174 175 176 177 178 179 180 
181 182 183 184 185 186 187 188 189 190 191 192 
193 194 195 196 197 198 199 

Note: This won't look right if you are using proportional-spaced font.
You need to use a console font where all the letters have equal width. e.g. Lucida Console, Courier New, monospace

This may still not work properly ... I have to check. I remeber there being a problem with doing this with printf, but I can't remember what.