Mod Division in Java

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

division

From school days we are familiar with the concept of the regular division:

6 / 2 =3,

12 / 6 =2

And so on.

Everything is clear with it. But what is the mod division? It even sounds a little bit threatening. In fact, it’s very and very easy. Let’s get to the bottom of it! You need to understand, that:

  1. Mod division is an operator

You should already know how the operators of addition, subtraction, etc. work. In few minutes you will understand for what the mod division is responsible. All that is required is a little patience.

operators-vertex-academy-en

  1. Mod division is designated as %
  2. Sometimes mod division is called mod. So if you see mod – we are talking about the Modulus Operator.
  3. What is the essence of the operator? The mod division returns the remainder of the division.

Example No.1

It is necessary to divide 9 in 4, using:

  • common division, as we were taught in school
  • mod division

 

You already understand how the modulus operator works. It’s a high time to run the example on your computer:

If you try to run this code on your computer, you will see the following number in your console:

1

Example No.2

It is necessary to divide 17 in 5, using:

  • common division, as we were taught in school
  • mod division

And let’s try to 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

It is necessary to divide 21 in 7, using:

  • common division, as we were taught in school
  • mod division

 

And let’s try to 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

It is necessary to divide 7.6 in 2.9, using:

  • common division, as we were taught in school
  • mod division

And let’s try to run this program on your computer:

If you try to run this code on your computer, you will see the number close to 1.8 in the console. For example, you can see the following number: 1.7999999999999998. Due to the specific features of Java, which we will cover later in our other articles, the number on different computers can be slightly different. But it will be close to the value 1.8.

So, as you already understand, the modulus operator calculates the remainder of the division.

  1. It applies to the following types of variables:
  • Byte, short, Int, long – integer variables
  • Float, Double – floating point numbers
  • Negative and positive numbers

There is a small particularity in the context of using the modulus operator with negative and positive numbers.

The simple rule works:

1. Put away the minus sign

2. Divide the numbers as usual

3. And then, if the first number (the dividend) has the minus sign, you add the minus sign to the result.

Example No.5

mod-example5-vertex-academy-en

And now let’s try to run the program on the computer – one of the examples described above:

If you try to run this code on your computer, you will see the following number in the console:

-1

That’s it, now you know what the mod division in Java is.

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Leave a Reply

Your email address will not be published.

Самоучители--узнать детальнее--