Prodigy Prodigy - 3 months ago 11
C# Question

C# fahrenheit to celsius

public static void Main()
{
WriteLine("Farenheit Here>>");
int F = Int32.Parse(ReadLine());

Double FtoC = (5.0 / 9.0) * (F - 32);
WriteLine("the celsius is {0} ", FtoC);
}


The input works for whole numbers but I also want it to work for Decimal numbers. Ex. If I were to type in 10 it will give me an answer, but if I were to type in 10.5 it will stop working. I am indeed new to C#.

Answer

you need to use the number parser you want, currently you are using int

Int32.Parse(ReadLine()); 

You can use double, or decimal (I'd suggest using decimal over double)

decimal F = Decimal.Parse(ReadLine());

then change to

decimal FtoC = (5.0M / 9.0M) * (F - 32M);
WriteLine("the celsius is {0} ", FtoC)

(the M is used to define decimal literals)