How to find the roots of a quadratic equation in VB.NET

6 views

```Imports System

Public Class Program
Public Shared Sub quadratic_equation_roots(ByVal a As Double, ByVal b As Double, ByVal c As Double)
Dim discriminant As Double = (b * b) - (4 * a * c)
Dim root1, root2 As Double

If discriminant > 0 Then
root1 = (-b + Math.Sqrt(discriminant)) / (2 * a)
root2 = (-b - Math.Sqrt(discriminant)) / (2 * a)
Console.WriteLine("root1 = " & root1 & Environment.NewLine & "root2 = " & root2)
ElseIf discriminant = 0 Then
root1 = -b / (2 * a)
root2 = -b / (2 * a)
Console.WriteLine("root1 = root2 = " & root1)
ElseIf discriminant < 0 Then
Dim real As Double = -b / (2 * a)
Dim imaginary As Double = Math.Sqrt(-discriminant) / (2 * a)
Console.WriteLine("root1 = " & real & "+" & imaginary & "i" & Environment.NewLine & "root2 = " & real & "-" & imaginary & "i")
End If
End Sub

Public Shared Sub Main()
Dim a As Double = 3, b As Double = 5, c As Double = -9
Console.WriteLine()

a = 3
b = 5
c = 7
Console.WriteLine()

a = 2
b = 4
c = 2
End Sub
End Class

' run:
'
' root1 = 1.08876043244513
' root2 = -2.7554270991118

' root1 = -0.833333333333333+1.2801909579781i
' root2 = -0.833333333333333-1.2801909579781i

' root1 = root2 = -1
'
```