Write a visual basic code that will compute root of a quadratic equation using determinant
Module Module1
''' <summary>
''' A function named getDiscriminant() that returns the discriminant, which is b2 - 4ac.
''' </summary>
''' <param name="a"></param>
''' <param name="b"></param>
''' <param name="c"></param>
''' <returns></returns>
''' <remarks></remarks>
Function getDiscriminant(ByVal a As Double, ByVal b As Double, ByVal c As Double) As Double
Return b * b - 4 * a * c
End Function
''' <summary>
''' The functions named getRoot1() and getRoot2() for returning two roots of the equation:
''' These functions are useful only if the discriminant is non-negative.
''' Let these functions return 0 if the discriminant is negative.
''' </summary>
''' <param name="a"></param>
''' <param name="b"></param>
''' <param name="c"></param>
''' <param name="root"></param>
''' <returns></returns>
''' <remarks></remarks>
Function getRoot1(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal root As Integer) As Double
Dim D As Double = getDiscriminant(a, b, c)
If (D < 0) Then
Return 0
End If
If root = 1 Then
Return ((-1) * b + Math.Sqrt(D)) / (2 * a)
End If
Return ((-1) * b - Math.Sqrt(D)) / (2 * a)
End Function
Sub Main()
Console.Write("Enter a: ")
Dim a As Double = Double.Parse(Console.ReadLine())
Console.Write("Enter b: ")
Dim b As Double = Double.Parse(Console.ReadLine())
Console.Write("Enter c: ")
Dim c As Double = Double.Parse(Console.ReadLine())
Dim D As Double = getDiscriminant(a, b, c)
'If the discriminant is positive, display the two roots.
If (D > 0) Then
Console.WriteLine("Root 1 = " + getRoot1(a, b, c, 1).ToString())
Console.WriteLine("Root 2 = " + getRoot1(a, b, c, 2).ToString())
ElseIf (D = 0) Then
'If the discriminant is 0, display the one root.
Console.WriteLine("Root 1 = " + getRoot1(a, b, c, 1).ToString())
Else
'Otherwise, display "The equation has no real roots".
Console.WriteLine("The equation has no real roots")
End If
Console.ReadLine()
End Sub
End Module
Comments
Leave a comment