0

why is this 12.0 instead of 12.5, i did the math on calc comes out to 12.5

int x, y; double z; x = 5; y = x * 2; z = x/2 + y; System.out.print(z);

19th Jul 2018, 4:50 PM
ralph
ralph - avatar
4 Answers
0
There fore, to get the required result, your variable x should also be a double, and not int
19th Jul 2018, 4:57 PM
Vedant Bang
Vedant Bang - avatar
0
x is an integer. When you do x/2 the output is 2, (as opposed to 2.5) because the integer cannot store decimal values.
19th Jul 2018, 4:57 PM
Vedant Bang
Vedant Bang - avatar
0
thanks!!
19th Jul 2018, 4:57 PM
ralph
ralph - avatar
0
You're welcome 😁
19th Jul 2018, 4:58 PM
Vedant Bang
Vedant Bang - avatar