I am new to coding in general, and especially new to C#. I am trying to write a program that asks the user if they want to convert a temperature to Fahrenheit or Celsius. The issue is, the math isn't checking out
This is what I have so far, and from what I know, it should work:
//declare variables
double temp = 0;
double newTemp = 0;
string? cf = "";
//ask if they want to covert to celcius or farenheit
Console.WriteLine("What are you converting to? Enter c for celsius or f for farenheit:");
cf = Console.ReadLine();
//if statement and output
if(cf == "c")
{
Console.WriteLine("Converting to Celsius. Enter temperature in farenheit:");
temp = Convert.ToDouble(Console.ReadLine());
newTemp = (temp - 32) * (5/9);
Console.WriteLine("Your temperature in celcius is " + newTemp + " degrees celsius.");
}
else if(cf == "C")
{
Console.WriteLine("Converting to Celsius. Enter temperature in farenheit:");
temp = Convert.ToDouble(Console.ReadLine());
newTemp = ((temp - 32) * (5/9));
Console.WriteLine("Your temperature in celcius is " + newTemp + " degrees celsius.");
}
else if(cf == "f")
{
Console.WriteLine("Converting to Farenheit. Enter temperature in celsius:");
temp = Convert.ToDouble(Console.ReadLine());
newTemp = (temp * (9/5) + 32);
Console.WriteLine("Your temperature in farenheit is " + newTemp + " degrees farenheit.");
}
else if(cf == "F")
{
Console.WriteLine("Converting to Farenheit. Enter temperature in celsius:");
temp = Convert.ToDouble(Console.ReadLine());
newTemp = (temp * (9/5) + 32);
Console.WriteLine("Your temperature in farenheit is " + newTemp + " degrees farenheit.");
}
else
{
Console.WriteLine("That is not celcius or farenheit. Please enter either c or f next time.");
Environment.Exit(0);
}
For some reason that I cant figure out, the math is always wrong. -40 degrees is never equal to -40 degrees in the inverse temperature, meaning that the math is incorrect. Would anyone be able to explain to me, in beginners terms, what the issue is and how I might resolve it?
P.S. - in many forums I looked at first, they used a term called floating-point that I don't really understand. If you use this in your answer, I would really appreciate an explanation on what it means.