How to Program Quadratic Equations solver (VB)?

Danial-Softwares

How to Program Quadratic Equations solver (VB)?

How to Program Quadratic Equations solver (VB)?

Hello all. This is our first tutorial after a long time of being under maintenance. In this tutorial we are going to learn how to program an app in visual basic that can solve quadratic equations.

A quadratic equation is a second-degree polynomial that has the form “ax^2 + bx + c = 0.” The “a,” “b” and “c” are the constants and “x” is the variable. When you solve a quadratic equation, you must have the values of the constants and solve for x, which always yields two values, called the “roots.” In Visual Basic, you can write a program or function that prompts the user to enter the a, b and c values, find the roots and then display the values on the form.

Lets start
 

  1. Add 3 textbox and Change the name of them to TxtA1 and TxtB1 TxtC1
  2. Add a label and name it to LblM1
  3. Add 6 labels and name them to Labelx , Labelx2 , lbldelta2 , lbldelta , LblAns2 and LblAns1
  4. Set the visible of these 6 labels to False
  5. Add a button and name it to Cmd1
  6.  

    vb objects for quadratic equations

     

  7.  Now just paste this code. ( I explained the codes function in green line)

Private Sub Cmd1_Click()
Dim delta As Double, a As Double, b As Double, c As Double

If TxtA1.Text = "" Or TxtB1.Text = "" Or TxtC1.Text = "" Then
MsgBox ("Please fill all informations")
Else

a = Val(TxtA1.Text)
b = Val(TxtB1.Text)
c = Val(TxtC1.Text)
delta = (b * b) - (4 * a * c)
lbldelta.Caption = delta      'it shows the Amount of delta
lbldelta.Visible = True
LblM1.Caption = TxtA1.Text + "x + " + TxtB1.Text + "X +" + TxtC1.Text   'This is for showing the form of equation
lbldelta2.Visible = True

If (delta > 0) Then       'so we have 2 roots
LblAns1.Caption = Str(((-1 * b) + Sqr(delta)) / (2 * a))
LblAns2.Caption = Str(((-1 * b) - Sqr(delta)) / (2 * a))
'lets make the roots visible
LblAns1.Visible = True
LblAns2.Visible = True
Labelx.Visible = True
Labelx2.Visible = True

End If

If (delta = 0) Then   'then we just have 1 root

LblAns1.Caption = (-1 * b) / (2 * a)
LblAns1.Visible = True
Labelx.Visible = True
' we dont have 2 roots so we have to set the visible of x2 and Ans2 False
LblAns2.Visible = False
Labelx2.Visible = False
End If
If (delta < 0) Then

LblAns1.Caption = "No Answer"
LblAns1.Visible = True
LblAns2.Visible = False
Labelx.Visible = False
Labelx2.Visible = False
End If

End If
End Sub

 
Coded By: Danial Nikbin

2 Comments

  1. Nice tutorial but it’s a little confusing for me 😀

    Reply
  2. Nice program, reminds me a lot of my calculator program because it was coded in Visual Basic. Nice work, keep it up founder. 😀

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.