Solve quadratic equations instantly with this interactive tool
A quadratic equation is a second-degree polynomial equation in a single variable with the form:
where x represents an unknown variable, and a, b, and c are coefficients with a ≠ 0.
The solutions to the quadratic equation are given by the quadratic formula:
The expression under the square root (b² - 4ac) is called the discriminant, which determines the nature of the roots:
Below are the Visual Basic 6 and VB.NET implementations of the quadratic equation solver:
Private Sub Form_Load() Dim a, b, c, det As Integer Dim root1, root2 As Single Dim numroot As Integer End Sub Private Sub new_Click() ' To set all values to zero Coeff_a.Text = "" Coeff_b.Text = "" Coeff_c.Text = "" Answers.Caption = "" txt_root1.Visible = False txt_root2.Visible = False txt_root1.Text = "" txt_root2.Text = "" Lbl_and.Visible = False Lbl_numroot.Caption = "" End Sub Private Sub Solve_Click() a = Val(Coeff_a.Text) b = Val(Coeff_b.Text) c = Val(Coeff_c.Text) 'To compute the value of the determinant det = (b ^ 2) - (4 * a * c) If det > 0 Then Lbl_numroot.Caption = 2 root1 = (-b + Sqr(det)) / (2 * a) root2 = (-b - Sqr(det)) / (2 * a) Answers.Caption = "The roots are " Lbl_and.Visible = True txt_root1.Visible = True txt_root2.Visible = True txt_root1.Text = Round(root1, 4) txt_root2.Text = Round(root2, 4) ElseIf det = 0 Then root1 = (-b) / 2 * a Lbl_numroot.Caption = 1 Answers.Caption = "The root is " txt_root1.Visible = True txt_root1.Text = root1 Else Lbl_numroot.Caption = 0 Answers.Caption = "There is no root " End If End Sub
Public Class QuadraticSolver Private Sub btnSolve_Click(sender As Object, e As EventArgs) Handles btnSolve.Click Dim a, b, c, discriminant, root1, root2 As Double ' Get coefficients from textboxes If Not Double.TryParse(txtA.Text, a) OrElse Not Double.TryParse(txtB.Text, b) _ OrElse Not Double.TryParse(txtC.Text, c) Then MessageBox.Show("Please enter valid numbers") Return End If ' Check if a is zero If a = 0 Then MessageBox.Show("Coefficient a cannot be zero") Return End If ' Calculate discriminant discriminant = b * b - 4 * a * c ' Determine roots based on discriminant If discriminant > 0 Then ' Two real roots root1 = (-b + Math.Sqrt(discriminant)) / (2 * a) root2 = (-b - Math.Sqrt(discriminant)) / (2 * a) lblResult.Text = $"Two real roots: {root1:F4} and {root2:F4}" ElseIf discriminant = 0 Then ' One real root root1 = -b / (2 * a) lblResult.Text = $"One real root: {root1:F4}" Else ' Complex roots Dim realPart = -b / (2 * a) Dim imaginaryPart = Math.Sqrt(-discriminant) / (2 * a) lblResult.Text = $"Two complex roots: {realPart:F4} + {imaginaryPart:F4}i and {realPart:F4} - {imaginaryPart:F4}i" End If End Sub Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click ' Clear all inputs and results txtA.Text = "" txtB.Text = "" txtC.Text = "" lblResult.Text = "" End Sub End Class
The quadratic equation solver uses the discriminant (b² - 4ac) to determine the nature of the roots:
The discriminant (D = b² - 4ac) is calculated first. This value determines the number and type of roots.
When discriminant is positive, two distinct real roots exist:
x₁ = [-b + √D]/(2a)
x₂ = [-b - √D]/(2a)
When discriminant is zero, one real root (repeated) exists:
x = -b/(2a)
When discriminant is negative, two complex roots exist:
x = [-b ± i√|D|]/(2a)
Master Visual Basic programming with our comprehensive guide that covers both VB6 and VB.NET. This book includes:
Whether you're maintaining legacy systems or developing modern applications, this book is your complete resource for Visual Basic programming.
Buy Paperback Get Kindle Edition