-3
private void btnCompute_Click(object sender, EventArgs e)
{
    double coefficientA;
    double coefficientB;
    double coefficientC;
    double root1;
    double root2;
    double discriminant;

    coefficientA = double.Parse(txtCoeA.Text);
    coefficientB = double.Parse(txtCoeB.Text);
    coefficientC = double.Parse(txtCoeC.Text);

    discriminant = 
            (coefficientB * coefficientB) - 
            (4 * coefficientA * coefficientC);

    txtOutput.Text = "Discriminant = " + discriminant.ToString("N2");

    //-b/2a

    root1 = (-coefficientB + discriminant) / (2 * coefficientA);
    root2 = (-coefficientB - discriminant) / (2 * coefficientA);

    if (discriminant < 0)
    {
            txtOutput.Text += "\r\nEquation has no real roots!";
    }
    else if (discriminant == 0)
    {
        txtOutput.Text += 
            "\r\nEquation has one root: " + root2.ToString("N2");
    }        
    else if (discriminant > 0)
    {
        txtOutput.Text = "Equation has two real roots: " +
            "\r\nroot1: " + 
            (-coefficientB + Math.Sqrt(discriminant) / 
            (2 * coefficientA)) +
            "\r\nroot2: " + (coefficientB - Math.Sqrt(discriminant) / 
            (2 * coefficientA)); 
    }
}
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
Samantha
  • 3
  • 2

1 Answers1

0

Try this:

    txtOutput.Text = "Equation has two real roots: " +
        "\r\nroot1: " + 
        (-coefficientB + Math.Sqrt(discriminant)) / (2 * coefficientA) +
        "\r\nroot2: " +
        (-coefficientB - Math.Sqrt(discriminant)) / (2 * coefficientA);

You were bracketing the formula incorrectly.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172