C program to convert Fahrenheit to Celsius

2019-01-01 02:45发布

I'm writing a program for a class I'm in and 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.

标签: c
8条回答
君临天下
2楼-- · 2019-01-01 03:19
using System;


public class Calculate
{

public static void Main(string[] args)
{
    //define variables
    int Celsius;
    int fahrenheit;
    string input;

    //prompt for input
    //read in the input and convert
    Console.WriteLine("Enter Celsius temperature");
    input = Console.ReadLine();
    Celsius = Convert.ToInt32(input);

    //calculate the result
    fahrenheit = ((Celsius * 9 )/5) + 32;

    //print to screen the result
    Console.WriteLine("32 degrees Celsius is {0}", "equivilant to 89.60 degrees fahrenheit");

    Console.ReadLine();
}
查看更多
十年一品温如言
3楼-- · 2019-01-01 03:21

write 5/9.0 instead of 5/9 -- this forces double division

查看更多
登录 后发表回答