I have a problem with this
System.out.print((9/5) * 3);
I expects the result 5.4
but it returns 3
. Why is this happening?
I have a problem with this
System.out.print((9/5) * 3);
I expects the result 5.4
but it returns 3
. Why is this happening?
9/5
is evaluated in integer arithmetic, so it's equal to 1.
Writing 9.0 / 5 * 3
is a common fix. (Promoting one of the coefficients in the term to a double
forces the evaluation to take place in double precision floating point.)
In java when you make division of integers it will return an integer not a real number :
9/5
return 1
instead of 1.8
to solve your problem you have two solutions :
9.0/5
((double)9/5) * 3
All the numbers are integer there so it will give integer result you may make 9.0 or 5.0 to get that answer... As given below..
System.out.print((9.0/5) * 3); OR
System.out.print((9/5.0) * 3);