Quadratic function
Write a program in VB that calculates and prints the roots of a quadratic
equation using the formula:
Understand the quadratic formula
−𝑏 ± √𝑏 2 − 4𝑎𝑐
2𝑎
In the program add 5 labels, 5 textboxes, 1 button and do setting as
below:
Object Property Settings
Label 1 Name lblcoeffa
Text Coefficient a
Label 2 Name lblcoeffb
Text Coefficient b
Label 3 Name lblcoeffc
Text Coefficient c
Label 4 Name lblroot1
Text Root 1
Label 5 Name lblroot2
Text Root 2
Textbox1 Name txtcoeffa
Textbox 2 Name txtcoeffb
Textbox3 Name txtcoeffc
Textbox 4 Name txtroot1
Textbox 5 Name txtroot2
Button Name btncalculate
Text Calculate
Form Name frmquadraticfunction
Text Quadratic Function
Double click the button and write the following code
Public Class frmquadraticfunction
Private Sub btncalculate_Click(sender As Object, e As EventArgs)
Handles btncalculate.Click
'declaration of variables
Dim a, b, c, determ As Integer
Dim root1, root2 As Single
'convert cooefficients of a,b,cinput to numeric values
a = Val(txtcoeffa.Text)
1|Assignment 2
b = Val(txtcoeffb.Text)
c = Val(txtcoeffc.Text)
' compute the value inside the square root defined as determinant
determ = (b ^ 2) - (4 * a * c)
'using sqrt function to find square root
'add or subtr the value to -b then divide by 2a
'the answer is assigned to root1 and root2 respectively
root1 = (-b + Math.Sqrt(determ)) / (2 * a)
root2 = (-b - Math.Sqrt(determ)) / (2 * a)
'assign the root to output textboxes after rounding off to 2 d.p.
txtroot1.Text = Math.Round(root1, 2)
txtroot2.Text = Math.Round(root2, 2)
End Sub
End Class
The solution will look like this
2|Assignment 2