Question: I am trying to make a program that calculates the answer of a quadratic equation with the general formula but i am encountering a few errors.
The way I have my Windows Form Application set up it asks for a, b, and c, and substitutes them in the general formula.
I have 3 text boxes, one for each value of a, b and c and one more for the answer, and it is supposed to work with a button I called "calculate". 
My problem is that when I try something other than a perfect trinomial square, the answer comes up as NaN.
Here is some of the code I have:

```
private void textBox1_TextChanged(object sender, EventArgs e)
    {
        a = Double.Parse(textBox1.Text);
    }

```

^ This is how I am assigning values to the variables

```
double sqrtpart = b * b - 4 * a * c;
        answer = (b + Math.Sqrt(sqrtpart)) / 2 * a;
        textBox4.Text = answer.ToString();

```

Answer: 
First, make sure that 4ac < b². The quadratic formula also utilizes the ± sign, so we must make two separate variables to hold the two different answers.

```
double sqrtpart = (b * b) - (4 * a * c);
answer1 = ((-1)*b + Math.Sqrt(sqrtpart)) / (2 * a);
answer2 = ((-1)*b - Math.Sqrt(sqrtpart)) / (2 * a);
textBox4.Text = answer1.ToString() + " and " + answer2.ToString();

```
Use the following code:

```
using System;
using System.Collections.Generic;
using System.Text;

namespace SoftwareAndFinance
{
    class Math
    {

            // quadratic equation is a second order of polynomial       equation in       a single variable 
        // x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
        public static void SolveQuadratic(double a, double b, double c)
        {
            double sqrtpart = b * b - 4 * a * c;
            double x, x1, x2, img;
            if (sqrtpart > 0)
            {
                x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
                x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a);
                Console.WriteLine("Two Real Solutions: {0,8:f4} or  {1,8:f4}", x1, x2);
            }
            else if (sqrtpart < 0)
            {
                sqrtpart = -sqrtpart;
                x = -b / (2 * a);
                img = System.Math.Sqrt(sqrtpart) / (2 * a);
                Console.WriteLine("Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i", x, img, x, img);
            }
            else
            {
                x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
                Console.WriteLine("One Real Solution: {0,8:f4}", x);
            }
        }

        static void Main(string[] args)
        {

            // 6x^2 + 11x - 35 = 0
            SolveQuadratic(6, 11, -35);

            // 5x^2 + 6x + 1 = 0
            SolveQuadratic(5, 6, 1);

            // 2x^2 + 4x + 2 = 0
            SolveQuadratic(2, 4, 2);

            // 5x^2 + 2x + 1 = 0
            SolveQuadratic(5, 2, 1);
        }
    }
}

```

Here is the original [source](http://www.softwareandfinance.com/CSharp/Solve_Quadratic_Equation.html).