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
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
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
Now modify your output
statement(s) or add a new one so that it will compute
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
y. For these real-number variables, you shouldn't compute
% 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
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
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
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
Question #3.3.4: Use your observations from the preceding question to fill in the following table:
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
numberis a multiple of 3.
Question #3.3.6: Write a boolean expression that can be used to check if an integer
numberis 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
numberis 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
numberis an odd number.