C# Find Roots of a Quadratic Equation

quadratic equation (from the Latin quadratus for “square”) is any equation having the form

{\displaystyle ax^{2}+bx+c=0,}

where x represents an unknown, and ab, and c represent known numbers, with a ≠ 0. If a = 0, then the equation is linear, not quadratic. The numbers ab, and c are the coefficients of the equation and may be distinguished by calling them, respectively, the quadratic coefficient, the linear coefficient and the constant or free term.

Because the quadratic equation involves only one unknown, it is called “univariate”. The quadratic equation only contains powers of x that are non-negative integers, and therefore it is a polynomial equation. In particular, it is a second-degree polynomial equation, since the greatest power is two. (wiki)

 

using System;

namespace Program
{
    class Quadraticroots
    {
        double a, b, c;

        public void Read()
        {
            Console.WriteLine("To find the roots of a quadratic equation of the form a*x*x + b*x + c = 0");
            Console.Write("\nEnter value for a: ");
            a = double.Parse(Console.ReadLine());

            Console.Write("\nEnter value for b: ");
            b = double.Parse(Console.ReadLine());

            Console.Write("\nEnter value for c: ");
            c = double.Parse(Console.ReadLine());
        }

        public void Compute()
        {
            int m;
            double r1, r2, d1;
            d1 = b * b - 4 * a * c;
            if (a == 0)
                m = 1;
            else if (d1 > 0)
                m = 2;
            else if (d1 == 0)
                m = 3;
            else
                m = 4;
            switch (m)
            {
                case 1:
                    Console.WriteLine("\n Not a Quadratic equation, Linear equation");
                    Console.ReadLine();
                    break;
                case 2:
                    Console.WriteLine("\n Roots are Real and Distinct");
                    r1 = (-b + Math.Sqrt(d1)) / (2 * a);
                    r2 = (-b - Math.Sqrt(d1)) / (2 * a);
                    Console.WriteLine("\n First root is {0:#.##}", r1);
                    Console.WriteLine("\n Second root is {0:#.##}", r2);
                    Console.ReadLine();
                    break;
                case 3:
                    Console.WriteLine("\n Roots are Real and Equal");
                    r1 = r2 = (-b) / (2 * a);
                    Console.WriteLine("\n First root is {0:#.##}", r1);
                    Console.WriteLine("\n Second root is {0:#.##}", r2);
                    Console.ReadLine();
                    break;
                case 4:
                    Console.WriteLine("\n Roots are Imaginary");
                    r1 = (-b) / (2 * a);
                    r2 = Math.Sqrt(-d1) / (2 * a);
                    Console.WriteLine("\n First root is {0:#.##} + i {1:#.##}", r1, r2);
                    Console.WriteLine("\n Second root is {0:#.##} - i {1:#.##}", r1, r2);
                    Console.ReadLine();
                    break;
            }
        }
    }

    class Roots
    {
        public static void Main()
        {
            Quadraticroots qr = new Quadraticroots();
            qr.Read();
            qr.Compute();
        }
    }
}

 

Output