Today I'm going to teach you guys how to use division operator in Java. As we know division operator use to divide basically two number and get the answer. Symbol of division operator is / in java and most of other programming languages. In natural languages we use same symbol but there are some different ways people use to write it with numbers to symbolize division operation.
ex
½ 1/2
Following is some mathematical examples for how to use division operator
ex
1/2 = 0.5
3/4 = 0.75
2/4 = 0.5
Let's see how to do this
x = 1 and y = 2 so x/y must be 0.5 but we got 0 as the answer why is this happening ?
by default compiler take answers of integer variables' operation as int so int data type can't keep floating point so it will return the only integer value it has as 0
we can fix this by casting x and y to double.
If we use double variables we can also fix that issue.
See you guys in my next post bye ....
No comments:
Post a Comment