Why does division result in zero instead of a decimal?

When you do 5/9, 5 and 9 are both integers and integer division happens. The result of integer division is an integer and it is the quotient of the two operands. So, the quotient in case of 5/9 is 0 and since you multiply by 0, tempC comes out to be 0. In order to not have integer division, atleast one of the two operands must be float.

E.g. if you use 5.0/9 or 5/9.0 or 5.0/9.0, it will work as expected.


It looks like you have integer division in the second case:

tempC=((5/9)*(tempF-32))

The 5 / 9 will get truncated to zero.

To fix that, you need to make one of them a floating-point type:

tempC=((5./9.)*(tempF-32))

Tags:

C