# Solving Quadratic Equations in .NET

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

## Introduction

In algebra, a quadratic equation is any equation in the form of ax2 + bx + c = 0. In the previous expression, x represents an unknown, and a, b, and c represent known numbers such that a is not equal to 0. The numbers a, b, and c are the coefficients of the equation, and can be distinguished by calling them the quadratic coefficient, the linear coefficient, and the constant or free term.

## Our Project

Use Visual Studio to create either a VB.NET or a C# Windows Forms Application and design the form to resemble Figure 1.

Figure 1: Design

Please note that my naming of objects may be different than yours.

## Code

C#

```using System;
using System.Windows.Forms;
```

VB.NET

`Imports System.Math`

C#

```      private double sngCoEffA;
private double sngCoEffB;
private double sngCoEffC;

private double sngDiscriminant;

private double sngRoot1;
private double sngRoot2;
```

VB.NET

```   Private sngCoEffA As Double
Private sngCoEffB As Double
Private sngCoEffC As Double

Private sngDiscriminant As Double

Private sngRoot1 As Double
Private sngRoot2 As Double
```

In the preceding code, you have created the Coefficient objects, Discriminant objects, as well as the Root objects. Add the following code for the Calculate button.

C#

```      private void btnCalculate_Click(object sender, EventArgs e)
{

sngCoEffA = float.Parse(txtCoEfficient1.Text.ToString());
sngCoEffB = float.Parse(txtCoEfficient2.Text.ToString());
sngCoEffC = float.Parse(txtCoEfficient3.Text.ToString());

sngDiscriminant = (sngCoEffB * sngCoEffB) - (4 * sngCoEffA
* sngCoEffC);

if ((sngDiscriminant > 0))
{

sngRoot1 = (-sngCoEffB + Math.Sqrt(sngDiscriminant)) /
(2 * sngCoEffA);
sngRoot2 = (-sngCoEffB - Math.Sqrt(sngDiscriminant)) /
(2 * sngCoEffA);

txtRoot1.Text = sngRoot1.ToString();
txtRoot2.Text = sngRoot2.ToString();

}
else if ((sngDiscriminant == 0))
{

sngRoot1 = (-sngCoEffB + Math.Sqrt(sngDiscriminant)) /
(2 * sngCoEffA);

MessageBox.Show("One Root");

txtRoot1.Text = sngRoot1.ToString();

}
else
{

MessageBox.Show("No Root");

}
}
```

VB.NET

```   Private Sub btnCalculate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click

sngCoEffA = txtCoEfficient1.Text
sngCoEffB = txtCoEfficient2.Text
sngCoEffC = txtCoEfficient3.Text

sngDiscriminant = (sngCoEffB * sngCoEffB) - (4 * sngCoEffA _
* sngCoEffC)

If (sngDiscriminant > 0) Then

sngRoot1 = (-sngCoEffB + Math.Sqrt(sngDiscriminant)) / _
(2 * sngCoEffA)
sngRoot2 = (-sngCoEffB - Math.Sqrt(sngDiscriminant)) / _
(2 * sngCoEffA)

txtRoot1.Text = sngRoot1
txtRoot2.Text = sngRoot2

ElseIf (sngDiscriminant = 0) Then

sngRoot1 = (-sngCoEffB + Math.Sqrt(sngDiscriminant)) / _
(2 * sngCoEffA)

MessageBox.Show("One Root")

txtRoot1.Text = sngRoot1

Else

MessageBox.Show("No Root")

End If

End Sub
```

The code for the calculate button works out the Quadratic Equation based on the values that were input. When run, the Form might resemble Figure 2.

Figure 2: Running

## Conclusion

Quadratic Equations might seem scary and daunting, especially when complicated formulas are involved. Hopefully, in a follow-up article I can explain a different technique. Until then, happy coding!

Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).