-2

This is a c program which converts fahrenheit to celsius . When I write number 9 in decimal form float tempFahToCelsius= (tempFah - 32) * (5/9); to float tempFahToCelsius= (tempFah - 32) * (5/9.0);

output of the program is different.

If 5 is the entered value then the output in celsius is -0.000000

but if in the formula 5 or 9 or both are written as decimal then the output for 5 entered value in celsius is -15.

#include <stdio.h>
void main()
{
    float tempFah;
    
    printf("Enter value of temperature in fahrenheit\n");
    
    scanf("%f", &tempFah);
    
    float tempFahToCelsius= (tempFah - 32) * (5/9);
    
    printf("%f fahrenheit in celsius is %f\n",tempFah, tempFahToCelsius);
}
Pratyush
  • 11
  • 1
  • 3
    Yes integer division `5/9 == 0` in C and many other languages. – Mikael Öhman Aug 19 '23 at 16:11
  • 1
    In the expression `5/9`, both operands are integers, so it performs integer division, producing the integer result `0`. If either of them is a floating point number, e.g. `5.0` or `9.0`, then it does floating point division, producing a floating point result. – Tom Karzes Aug 19 '23 at 16:26
  • @Pratyush, aside: why code with `float` and not `double`? Note that `printf("%f fahrenheit in celsius is %f\n",tempFah, tempFahToCelsius);` converts `tempFah, tempFahToCelsius` to `double` before passing the arguments to `printf()`. – chux - Reinstate Monica Aug 19 '23 at 18:47
  • @chux-ReinstateMonica I didn’t know that float value is converted into double when passed to printf. – Pratyush Aug 22 '23 at 10:37

0 Answers0