Mod Division in Java

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

division

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:

  1. 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. 

operators-vertex-academy-en

  1. In division, we use "/", but in mod division, we use %
  2. Sometimes mod division is called mod. So if you see the word "mod," it means we're talking about the Modulus Operator.
  3. 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:

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:

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:

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:

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.

  1. 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

mod-example5-vertex-academy-en

And now let’s run the program with one of the examples above:

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.

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr