VB Tutor VB2022 VB2019 VB6 VB Sample Code About Us
Quadratic Equation Solver

Quadratic Equation Solver

Solve quadratic equations instantly with this interactive tool


A quadratic equation is a second-degree polynomial equation in a single variable with the form:

ax² + bx + c = 0

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:

x = [-b ± √(b² - 4ac)] / (2a)

The expression under the square root (b² - 4ac) is called the discriminant, which determines the nature of the roots:

  • Positive discriminant → Two distinct real roots
  • Zero discriminant → One real root (repeated)
  • Negative discriminant → Two complex roots
Quadratic Formula

Interactive Quadratic Equation Solver

Solution:

Implementation Code

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

How the Quadratic Solver Works

The quadratic equation solver uses the discriminant (b² - 4ac) to determine the nature of the roots:

Discriminant Calculation

The discriminant (D = b² - 4ac) is calculated first. This value determines the number and type of roots.

Real Roots (D > 0)

When discriminant is positive, two distinct real roots exist:
x₁ = [-b + √D]/(2a)
x₂ = [-b - √D]/(2a)

Repeated Root (D = 0)

When discriminant is zero, one real root (repeated) exists:
x = -b/(2a)

Complex Roots (D < 0)

When discriminant is negative, two complex roots exist:
x = [-b ± i√|D|]/(2a)

Note: The coefficient 'a' must be non-zero for the equation to be quadratic. If a=0, the equation becomes linear.

Learn More with Our Book

Visual Basic Programming Book

Visual Basic Programming with Code Examples

Master Visual Basic programming with our comprehensive guide that covers both VB6 and VB.NET. This book includes:

  • Step-by-step tutorials for beginners
  • 48 practical code examples with explanations
  • Dual-format code samples for VB6 and VB.NET
  • Projects including financial calculators, games, and database applications
  • Advanced topics for experienced developers

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