Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program to convert Fahrenheit to Celsius always prints zero

Tags:

c

I need some help with a program for converting Fahrenheit to Celsius in C. My code looks like this

#include <stdio.h>
int main(void)
{
    int fahrenheit;
    double celsius;

    printf("Enter the temperature in degrees fahrenheit:\n\n\n\n");
    scanf("%d", &fahrenheit);
    celsius = (5 / 9) * (fahrenheit - 32);
    printf("The converted temperature is %lf\n", celsius);

    return 0;
}

Every time I execute it it the result is 0.000000. I know I'm missing something but can't figure out what.

like image 306
James Avatar asked Feb 03 '11 19:02

James


2 Answers

5/9 will result in integer division, which will = 0

Try 5.0/9.0 instead.

like image 199
nybbler Avatar answered Sep 28 '22 12:09

nybbler


You problem is here :

celsius = (5/9) * (fahrenheit-32);

5/9 will always give you 0. Use (5.0/9.0) instead.

like image 32
Incognito Avatar answered Sep 28 '22 11:09

Incognito