Term 1: Lesson 7 - Modular Division
Modular division is used for:
Patterns Tell even and odd numbers Time calculations Money Encryption on the Internet
Six divided by three is two
Six mod 2 is zero because there is no remainder.
Modular division will cause
an error if we try to divide by zero. This is a runtime error because we don't necessarily know when we compile the code whether the user will input a zero.
If you are owed $0.67 in change, which programming statement below would tell you how many quarters you should get? Assume that change is an int variable that stores the number of cents owed (67 in this case).
change / 25
If x mod 2 is zero, the number is
divisible by 2, and is even. Otherwise it's ood.
The following code does not work as intended. It is meant to input two test grades and return the average in decimal format: int test1 = scan.nextInt(); int test2 = scan.nextInt(); double average = (test1 + test2 )/2; System.out.println("Answer: " + average); Which of the following corrections will allow the code to work as intended?
double average = (double)(test1 + test2)/2;
Which line of code sets int x to 7 multiplied by 6?
int x = 7 * 6;
The result of a modular division can
never be bigger than or equal to the second number.
modular division is also known as
remainder division
Modular division still works if
the first number is smaller than the second number
Modular Division gives
the remainder when dividing two numbers
One of the most common uses of modular division is
to tell whether a number is even or odd
Modular division is only for
integers
To perform modular division in Java we use the _____ operator.
%
What is 55 % 5?
0
What is 57 % 4?
1
To find the ones digit of a number you MOD by _______.
10
What is x equal to after the following code has been executed: double x = 15.67; x = (int)x;
15.0
To tell if a number is even or odd you MOD by ______.
2
What is 3 % 5?
3
