Mod Division in Java
In elementary school, we learned division:
6 / 2 = 3,
12 / 6 = 2
And so on.
Division makes sense. But how about mod division? What is that? It even sounds a bit scary. But in fact, it’s very easy. Let’s learn about it. The first thing you need to understand is that:
- Mod division is an Operator
You already know how the addition and subtraction operators work. After you finish reading this article, you will also understand for what mod division does.
- In division, we use "/", but in mod division, we use %
- Sometimes mod division is called mod. So if you see the word "mod," it means we're talking about the Modulus Operator.
- What exactly does the operator do? The mod division gives the remainder of the division.
Example No. 1
You can divide 9 by 4, using:
- common division, as we were taught in school
- mod division
You now understand the logic of how the modulus operator works. Now it's time to run an example on your computer:
1 2 3 4 5 6 7 8 9 |
class Test { public static void main(String[] args) { int n = 9; int k = 4; int m = n%k; System.out.println(m); } } |
If you run this code on your computer, you will see the following number in your console:
1
Example No. 2
You can divide 17 by 5, using:
- common division, as we were taught in school
- mod division
And let’s run this program on the computer:
1 2 3 4 5 6 7 8 9 |
class Test { public static void main(String[] args) { int n = 17; int k = 5; int m = n%k; System.out.println(m); } } |
If you run this code on your computer, you will see the following number in the console:
2
Number No. 3
You can divide 21 by 7, using:
- common division, as we were taught in school
- mod division
And let’s run this program on the computer:
1 2 3 4 5 6 7 8 9 |
class Test { public static void main(String[] args) { int n = 21; int k = 7; int m = n%k; System.out.println(m); } } |
If you run this code on your computer, you will the following number in the console:
0
Example No. 4
You can divide 7.6 by 2.9, using:
- common division, as we were taught in school
- mod division
And let’s run this program on the computer:
1 2 3 4 5 6 7 8 9 |
class Test { public static void main(String[] args) { double n = 7.6; double k = 2.9; double m = n%k; System.out.println(m); } } |
If you run this code on your computer, you will see a number close to 1.8 in the console. You may see the number 1.7999999999999998. Due to the peculiarities of Java, which we will cover later in other articles, the number may be slightly different on different computers. But it will be close to the value 1.8.
So, as you now understand, the modulus operator gives the remainder of the division.
- Mod division applies to the following types of variables:
- Byte, short, Int, long – integer variables
- Float, Double – floating point numbers
- Negative and positive numbers
When you use the modulus operator with negative numbers, you have to follow some eccentric rules.
Take the following steps:
1. Remove the minus sign.
2. Divide the numbers as usual.
3. And then, if the first number (the dividend) had a minus sign in front of it (before you removed it), then you bring back the minus sign and put it in front of the result.
Example No. 5
And now let’s run the program with one of the examples above:
1 2 3 4 5 6 7 8 9 |
class Test { public static void main(String[] args) { int n = -9; int k = -4; int m = n%k; System.out.println(m); } } |
If you run this code on your computer, you will see the following number in the console:
-1
You now know what mod division in Java is.