-3

I am trying to write a program to convert fahrenheit to celsius. No matter what you input, c always outputs 0.

#include <stdio.h>

int main()
{
int f;
float c;

printf("Enter a temperature in fahrenheit: ");
scanf("%d", &f);

c = (f-32)*(5/9);

printf("%d fahrenheit is %.2f celsius.\n", f, c);
}    
David Vlcek
  • 21
  • 1
  • 6

1 Answers1

2

In integer math, 5/9 is 0, remainder 5. You want 5.0/9.0 instead.

In C, what you do with a result has no effect on how it's computed. So even though c is a float and you are storing the result of the math in a float, the math is still integer math because 5, 9, f and 32 are all integers.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278