Math operation does not work correctly in C #

I just started learning C #, I try a program that takes the temperature in degrees Fahrenheit and converts it to a target, and in the third step converts the target back to Fahrenheit (to check the math) I used the following code to execute the above function

Console.Write("Enter Your Temperature in fahrenheit  : ");
float faren = float.Parse(Console.ReadLine());
float cel = (faren - 32)*(5/9);
float farenconv = 32 + (cel * 9 / 5);
Console.WriteLine("Orignal Temperature : " + faren);
Console.WriteLine("Converted in celsuis : " + cel);
Console.WriteLine("Converted Back in fahrenheit  : " + farenconv);

Now the problem is what I get cel = 0as the output value no matter what I input as fahrenheit, but when I delete *(5/9), it works fine if there are any hints.

+4
source share
2 answers

Use 5 / 9f, which is a classic problem when we do calculations with integers.

, 5/9 0, a/b, a b , a<b 0.

+8

, , # , , . , . , , , , float. f , 9f float. , 9d double, 9m decimal.

,

float cel = (faren - 32)*(5/9f);
float farenconv = 32 + (cel * 9 / 5f);
+2

Source: https://habr.com/ru/post/1534226/


All Articles