why the output coming 37 not 41?

closed Nov 19
include <stdio.h>

int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
return 0;
}

answered Nov 13 by (40,320 points)
edited Nov 15 by Peter Minarik
```(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.

answered Nov 14 by (270 points)

#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;
}
commented Nov 16 by (160 points)
#include <stdio.h>

int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9.0/5.0)*c + 32);
return 0;
}
answered Nov 17 by (160 points)

#include <stdio.h>

int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9.0/5.0)*c + 32);
return 0;
}

answered Nov 17 by (140 points)

because its divide first 9/5

correct program is

#include <stdio.h>

int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9*c/5 )+ 32);
return 0;}