Quadratic Equation Solver

 

Quadratic equation is a fairly straight forward high school mathematics problem. The quadratic equation solver was programmed  to determine the number of roots the equation has as well as to compute the roots. It uses the determinant b2 -4ac to solve the problems. If b2 -4ac>0, then it has two roots and if b2 -4ac=0, then it has one root, else it has no root. To obtain the roots, the program uses the standard quadratic formula :

The  Interface

 

The Code

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


 

 

[Back to Sample VB Programs]

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact:admin@vbtutor.net

[Privacy Policy]