Solve Quadratic Equation Code and Tutorial in C#


You can very easily solve a Quadratic Equation with this small program. Just enter the values in the 3 text boxes and you will find the resulting roots(solutions) of the Quadratic equation on the right. Watch the final output and see the source code further below: 

Solve Quadratic Equation in C# WinForms Application

The C# Code below is simple, structured & complete. Note the code for the text box change event is similar for all the 3 text boxes. 


namespace Solve_Quadratic_Equation

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        double a, b, c;

Code to be used if the Determinant of Quadratic Equation is positive; This code returns the value to be used when the square root of the determinant is a real number. 

        public double DeterminantQuad(double b, double a, double c)

        {

            return ((b * b) - (4 * a * c));

        }

Code to be used if Determinant of Quadratic Equation is negative; The code below returns the negative value of the regular determinant. Since the regular determinant is negative, we get a positive value in the end(negative of negative is positive). We then add the letter "i" in the answer to show that the square root is taken of a negative number. Therefore pointing out the unreal root in red color. 

        public string NegativeDeterminant(double b, double a, double c)

        {

            return (((4 * a * c) - (b * b)).ToString());

        }

Calculate the First Root of the equation:
Quadratic Equation 1st Root C# Code

        public string FirstRealRootQuad(double a, double b, double c)

        {

            if (DeterminantQuad(b, a, c) >= 0)

            {

                return ((((-b) + (Math.Sqrt(DeterminantQuad(b, a, c)))) / (2 * a)).ToString());

            }

            else

            { return ((-b / (2 * a)).ToString() + "+ i" + (Convert.ToDouble(NegativeDeterminant(b, a, c)) / (2 * a)).ToString()); }
        }

Calculate the Second Root of the equation:
Quadratic Equation  2nd Root C# Code

        public string SecondRealRootQuad(double a, double b, double c)

        {

            if (DeterminantQuad(b, a, c) >= 0)

            {

                return ((((-b) - (Math.Sqrt(DeterminantQuad(b, a, c)))) / (2 * a)).ToString());

            }

            else

            { return ((-b / (2 * a)).ToString() + "- i" + (Convert.ToDouble(NegativeDeterminant(b, a, c)) / (2 * a)).ToString()); }

        }

TextBox Text Change Event Code; This code is similar for all the 3 Text Boxes

        private void QuadA_TextChanged(object sender, EventArgs e)

        {

            try

            {

                a = Convert.ToDouble(QuadA.Text);

                b = Convert.ToDouble(QuadB.Text);

                c = Convert.ToDouble(QuadC.Text);



                QuadRootA.Text = FirstRealRootQuad(a, b, c);

                QuadRootB.Text = SecondRealRootQuad(a, b, c);

            }

            catch { }



            if (QuadRootA.Text.Contains("i"))

            {

                QuadRootA.ForeColor = Color.Red;

            }

            if (QuadRootB.Text.Contains("i"))

            {

                QuadRootB.ForeColor = Color.Red;

            }

        }

}