Section 5c

Integer Math - Modulo Arithmetic


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Remainder Division

The rest of the story. So, if you can get a quotient out of integer math, and you can't get any fractional results, what do you do next? The answer is, you get the remainder. So, how do you get the remainder? Easy question, and easy answer. You conduct modulo math.

Consider the following: 17 ÷ 5 = 3 remainder 2

As mentioned previously, in integer arithmetic, any fractional parts are dropped or truncated; it is simply inappropriate to have a fractional part. Therefore, the integer division process 17 ÷ 5, which is actually 17 / 5 in program syntax, would result in 3, and would end there.

However, if in the course of conducting integer arithmetic, you would like to calculate the remainder, you would implement 17 mod 5, and the result would be 2. The operator used in C++ is the percent sign (i.e., '%'), which will be called the modulo operator for the remainder of this reference.

It is almost always important to work out modulo results on paper. Since it is not one of the arithmetic tools most people were provided, the first answer in your head may not be correct. Consider the following:

int result_1 = 5 % 3;

int result_2 = 3 % 5;

Should these have the same result? Let’s work them out.

__ _1_ _1_ _1_ R2
3 / 5 => 3 / 5 => 3 / 5 => 3 / 5
3 _3_
2

__ _0_ _0_ _0_ R3
5 / 3 => 5 / 3 => 5 / 3 => 5 / 3
0 _0_
3

Consider the following examples to help exercise your knowledge of the modulo operator. You will be using the modulo operation to implement several program control operations later on. However, that requires that you can figure out what the results will be before you ask the computer to do so. Work the following out yourself and make sure you get the same answers.

oneDivVal = 27 / 5; // result is 5
oneModVal = 27 % 5; // result is 2
secondDivVal = 33 / 8; // result is 4
secondModVal = 33 % 8; // result is 1
thirdDivVal = 41 / 6; // result is 6
thirdModVal = 41 % 6; // result is 5

If those worked for you, try these next few.

fourthDivVal = 140 / 12;
fourthModVal = 140 % 12;
fifthDivVal = 165 / 13;
fifthModVal = 165 % 13;
sixthDivVal = 99 / 10;
sixthModVal = 99 % 10;

Do not move forward unless you can understand how each of these solutions came out. This will become more important as your learningprogresses. One problem students have is attempting to conduct these calculations with a calculator. Unless your calculator has an integer mode, it will only serve to confuse you.

Watch this video and write the program with it. This will give you a tool for training yourself on modulo math with the bonus of having written another foundational program.


The End of the Beginning, of Math Operations

The problems offered here, and the calculations necessary for you to manage programs are pretty simple; you will always be able to conduct them on paper. As you now know, you have five operations you can conduct with integers. While you will certainly be using the four with which you are familiar, the modulo operation will also become a powerful tool later on when you get to making decisions in your program. Stay tuned.