using System;
namespace Test
{
class Roots
{
static double InputDouble(string title)
{
Console.Write(title);
string tmp = Console.ReadLine();
double result;
if(!double.TryParse(tmp, out result))
{
throw new Exception("Bad input");
}
return result;
}
static int Main()
{
double a = InputDouble("Enter the coefficient a: ");
double b = InputDouble("Enter the coefficient b: ");
double c = InputDouble("Enter the coefficient c: ");
double D = b*b - 4*a*c;
if(D < 0)
{
Console.WriteLine("Result: no real roots");
}
else
if(D == 0)
{
Console.WriteLine("Result: x = {0}", -b / (2*a));
}
else
{
double x1 = (-b + Math.Sqrt(D)) / (2*a);
double x2 = (-b - Math.Sqrt(D)) / (2*a);
Console.WriteLine("Result: x1 = {0}, x2 = {1}", x1, x2);
}
return 0;
}
}
}
Comments
Leave a comment