+4 votes

(9 / 5) * 5.0 + 32 = (int / int) * float + int '--v--' '----v----' 1 * 5.0 + 32 = int * float + int '----v---' '------v------' 5.0 + 32 = float + int '----v----' '-------v------' 37.0 = float

9/5 is an integer division (all fractions are ignored) which results in 1. If you want a float division (fractions included), then you have to have either the nominator or the denominator to be a float (or double). Either explicitly cast them to float ((float)9) or use a floating-point literal (e.g. 9.0 or 5.0), not an integral literal (9, 5).

Another solution is to use implicit casting, that is to let the compiler cast your number automatically due to operator precedence. In this case, you could reorder your expression to be something like this:

c * 9 / 5 + 32 = float * int / int + int --> float

This way, due to operator precedence, the result of each operation is a float, not an int.

For clarity, probably the best solution is to always use floating-point literals to explicitly state that you want the result to be a floating-point number and not an integral number.

0 votes

//please write your code properly, Your formula was wrong.

#include <stdio.h>

int main()

{

float c = 5.0;

printf ("Temperature in Fahrenheit is %f", (c*9/5) + 32); /*(9/5)*c+32) is wrong formula*/

return 0;

}

#include <stdio.h>

int main()

{

float c = 5.0;

printf ("Temperature in Fahrenheit is %f", (c*9/5) + 32); /*(9/5)*c+32) is wrong formula*/

return 0;

}

0 votes

#include <stdio.h>

int main()

{

float c = 5.0;

printf ("Temperature in Fahrenheit is %.2f", (9.0/5.0)*c + 32);

return 0;

}

...

int main()

{

float c = 5.0;

printf ("Temperature in Fahrenheit is %.2f", (9.0/5.0)*c + 32);

return 0;

}