C++ provides several arithmetic operators:

`+`

addition, computes the sum of two (integer or real) operands`-`

subtraction, computes the difference of two (integer or real) operands`*`

multiplication, computes the product of two (integer or real) operands`/`

division, computes the quotient of the division of two (integer or real) operands`%`

modulus, computes the remainder of the division of two integer operands

Question #3.3.1: The current version of our program outputs the sum of`i`

and`j`

. Modify it (or add more output statements) to output their difference and product. What changes did you make? Test your changes to make sure they are correct.

These arithmetic operators should be familiar to you, except for the last one. Let's spend some time looking at it and the corresponding division operator.

There is an important difference between the division of two integers and two real numbers. You will now explore these differences now by making some changes to our programs.

Make sure that your program still has an input statement to get values for
`i`

and `j`

and that it is between the declarations and
the output statement(s). With this input statement you can compile the
program *once* but execute it *many* times for different values
for `i`

and `j`

.

Now modify your output
statement(s) or add a new one so that it will compute `i/j`

and `i%j`

.* Be sure to change the string labels so that you
can read your output easily.*

Once you have these changes made, compile and execute the program to make sure it's correct.

Then, using this code as a basis, add
similar lines of code for `double`

variables `x`

and
`y`

. For these real-number variables, you shouldn't compute
`x%y`

because `%`

is only defined for integers..

Question #3.3.2: Use your program to fill in the following table with the values exactly as produced by your program:

`i`

`j`

`i / j`

`i % j`

`x`

`y`

`x / y`

a. 4 1 4.0 1.0 b. 4 2 4.0 2.0 c. 4 3 4.0 3.0 d. 4 4 4.0 4.0 e. 4 5 4.0 5.0 f. 4 6 4.0 6.0 g. 5 4 5.0 4.0 h. 6 4 6.0 4.0 i. 7 4 7.0 4.0

Integers are whole numbers *without any* fractional
part. So when you divide the integer 9 by the integer 10 (i.e,
`9/10`

), you will get 0, not 0.9. But with real numbers, you can
compute fractional amounts. So `9.0/10.0`

is `0.9`

as a real number.

Now, more about the modulus operation. "**Modulus
**" is basically just another name for
"**remainder**". When you first
learned about division, you probably learned to talk about your
result in terms of quotient and remainder:

"28 divided by 3 is 9 with a remainder of 1."

Division and modulus by 10 allow us to split up integers into their decimal digits.

Question #3.3.3: Use your program to fill in the following table with the values exactly as produced by your program:

`i`

`j`

`i / j`

`i % j`

a. 1234 1 b. 1234 10 c. 1234 100 d. 1234 1000

Can you see a pattern? How significant is the number of 0s in `j`

?

Question #3.3.4: Use your observations from the preceding question to fill in the following table:

`i`

`j`

`i / j`

`i % j`

a. 5678 1 b. 5678 10 c. 5678 100 d. 5678 1000

The multiples of 2 are 0, 2, 4, 6, 8, 10, 12, ... The multiples of 3 are 0, 3, 6, 9, 12, ...

What do the multiples of 2 have in common? Two evenly divides each
multiple of two; that is, two divides each multiple *without any
remainder*. So if `m % 2`

is 0, then `m`

must be a
multiple of two.

Using this same thinking, use `%`

to develop the tests
in the following questions.

Question #3.3.5: Write a boolean expression that can be used to check if an integer`number`

is a multiple of 3.

Question #3.3.6: Write a boolean expression that can be used to check if an integer`number`

is a multiple of 4.

The multiples of 2 are also known as the **even numbers**.

Question #3.3.7: Write a boolean expression that can be used to check if an integer`number`

is an even number.

The **odd numbers** are all the integers that aren't
even.

Question #3.3.8: Write a boolean expression that can be used to check if an integer`number`

is an odd number.

Back to the Lab Exercise | Forward to the Next Experiment

Report errors to Larry Nyhoff (nyhl@cs.calvin.edu)