Can You Divide a Double by an Int Java
In elementary school, we learned partition:
6 / 2 = 3,
12 / half dozen = two
And then on.
Sectionalization makes sense. Merely how about modern division ? What is that? It fifty-fifty sounds a bit scary. But in fact, it'south very like shooting fish in a barrel. Allow's acquire about it. The first thing you need to understand is that:
- Mod division is an Operator
Yous already know how the improver and subtraction operators work. After you lot finish reading this article, you volition besides empathize for what modernistic sectionalisation does.
- In partitioning, we utilize "/", but in mod division, we use%
- Sometimes modern partitioning is called mod. So if you see the word "mod," it ways we're talking nearly the Modulus Operator.
- What exactly does the operator exercise? The mod partitioning gives the remainder of the division.
Example No. ane
Yous tin split 9 by 4, using:
- common division, as nosotros were taught in school
- modernistic segmentation
You now sympathise the logic of how the modulus operator works. Now it'due south time to run an example on your figurer:
class Test { public static void main ( String [ ] args ) { int n = 9 ; int one thousand = 4 ; int m = n % k ; Organisation . out . println ( 1000 ) ; } } |
If y'all run this code on your computer, you volition see the post-obit number in your console:
1
Example No. 2
You can split 17 by 5, using:
- mutual sectionalisation, as nosotros were taught in school
- mod division
And let's run this program on the estimator:
class Examination { public static void main ( String [ ] args ) { int n = 17 ; int k = five ; int 1000 = n % one thousand ; Organization . 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 split up 21 past 7, using:
- common division, as nosotros were taught in school
- mod division
And let's run this program on the reckoner:
course Test { public static void main ( Cord [ ] args ) { int n = 21 ; int one thousand = 7 ; int grand = n % k ; System . out . println ( m ) ; } } |
If you run this code on your computer, you will the post-obit number in the console:
0
Example No. four
Yous can divide 7.6 past two.ix, using:
- common division, as nosotros were taught in schoolhouse
- modernistic division
And let's run this programme on the estimator:
class Test { public static void master ( String [ ] args ) { double north = 7.6 ; double g = ii.9 ; double m = n % k ; System . out . println ( m ) ; } } |
If you run this code on your computer, you lot will see a number close to 1.8 in the panel. Yous may see the number one.7999999999999998. Due to the peculiarities of Coffee, which we will embrace later in other articles, the number may be slightly dissimilar on unlike computers. But it will exist close to the value 1.viii.
Then, every bit you at present empathize, the modulus operator gives the remainder of the division .
- Mod division applies to the following types of variables:
- Byte, short, Int, long – integer variables
- Bladder, Double – floating point numbers
- Negative and positive numbers
When you use the modulus operator with negative numbers, you take to follow some eccentric rules.
Take the post-obit steps:
i. Remove the minus sign.
two. Divide the numbers every bit usual.
3. And then, if the first number (the dividend) had a minus sign in forepart of it (before yous removed information technology), then yous bring dorsum the minus sign and put information technology in front end of the result.
Example No. 5
And now let'due south run the program with ane of the examples to a higher place:
class Test { public static void main ( String [ ] args ) { int n = -9 ; int k = -4 ; int m = n % k ; Arrangement . out . println ( one thousand ) ; } } |
If y'all run this code on your computer, you will see the post-obit number in the console:
-1
You now know what mod division in Java is.
Source: https://vertex-academy.com/tutorials/en/dividing-module-java/
0 Response to "Can You Divide a Double by an Int Java"
Post a Comment