jloga4
12/17/2016 - 7:17 PM

How to parse input strings to a different type

How to parse input strings to a different type

Console.Write("a = ");
int a = int.Parse(Console.ReadLine());

Console.Write("b = ");
int b = int.Parse(Console.ReadLine());

Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
Console.WriteLine("{0} * {1} = {2}", a, b, a * b);

Console.Write("f = ");
double f = double.Parse(Console.ReadLine());

Console.WriteLine("{0} * {1} / {2} = {3}" ,
a, b, f, a * b / f);

// Can also use Convert.ToInt32(), Convert.ToDouble(), Convert.ToDecimal() etc.