INN = 9 / 2;
will assign 4.0
to INN
.
Replace it with INN = 9.0 / 2.0;
to assign 4.5
.
Explanation:
Because both 9
and 2
are integers, 9 / 2
always results in an integer division, the result of which is an integer too. Thus the result must be rounded, and is rounded down to the nearest integer.
Whereas 9.0
and 2.0
are doubles, thus 9.0 / 2.0
will result in a floating point division, which results in a double value.
2
solved Division not outputting correct answer c++