-3

I am just learning visual basic and have been trying to write a code to solve quadratic equations with complex number as roots. Any pointer in the right direction would be appreciated.

Public Class Form1
    Dim a, b, c As Integer
    Dim x1, x2 As Double

    Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click

    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles eqnRT1.TextChanged

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Close()
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtA.Text = ""
        txtB.Text = ""
        txtC.Text = ""
        eqnRT1.Text = ""
        eqnRT2.Text = ""
    End Sub

    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        Dim a,b,c as integer
        a = txtA.Text 
        b = txtB.Text 
        c = txtC.Text 

        Dim x1 = (-b + math.Sqrt(b * b - 4 * a * c)) / (2 * a)
        Dim x2 = (-b - math.Sqrt(b * b - 4 * a * c)) / (2 * a)
        eqnRT1.Text = Str(x1)
        eqnRT2.Text = Str(x2)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class
Blackwood
  • 4,504
  • 16
  • 32
  • 41
High Zedd
  • 53
  • 10

2 Answers2

0

There are quite a few answers on the internet that do this ...

    Sub Main()

    Dim a, b, c As Single
    Console.WriteLine("Write coefficient 'a'")
    a = Console.ReadLine
    Console.WriteLine("Write coefficient 'b'")
    b = Console.ReadLine
    Console.WriteLine("Write coefficient 'c'")
    c = Console.ReadLine

    Dim d As Integer = b ^ 2 - 4 * a * c
    If d >= 0 Then
        If d = 0 Then
            Console.WriteLine("Roots are real and equal")
        Else
            Console.WriteLine("Roots are real and different")
        End If

        Console.Write("Roots are: ")
        Console.Write((-b + d ^ 0.5) / (2 * a) & " , ")
        Console.WriteLine((-b - d ^ 0.5) / (2 * a))

    Else
        Console.WriteLine("Roots are complex")
        Console.Write("Roots are: ")
        Console.Write(-b / (2 * a) & "+" & (d * -1) ^ 0.5 / (2 * a) & "i")
        Console.Write(" , ")
        Console.WriteLine(-b / (2 * a) & "-" & (d * -1) ^ 0.5 / (2 * a) & "i")

    End If
    Console.ReadLine()

End Sub
Nefariis
  • 3,451
  • 10
  • 34
  • 52
0

This code seems to be working fine.

Public Class Form1
    Dim a, b, c As Integer
    Dim x1, x2 As Double

    Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles eqnRT1.TextChanged
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Close()
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtA.Text = ""
        txtB.Text = ""
        txtC.Text = ""
        eqnRT1.Text = ""
        eqnRT2.Text = ""
    End Sub

    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        Dim a = New System.Numerics.Complex(Double.Parse(txtA.Text), 0.0)
        Dim b = New System.Numerics.Complex(Double.Parse(txtB.Text), 0.0)
        Dim c = New System.Numerics.Complex(Double.Parse(txtC.Text), 0.0)
        Dim x1 = (-b + System.Numerics.Complex.Sqrt(b * b - 4 * a * c)) / (2 * a)
        Dim x2 = (-b - System.Numerics.Complex.Sqrt(b * b - 4 * a * c)) / (2 * a)
        eqnRT1.Text = String.Format("{0:0.00}+{1:0.00}i", x1.Real, x1.Imaginary)
        eqnRT2.Text = String.Format("{0:0.00}+{1:0.00}i", x2.Real, x2.Imaginary)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub
End Class
Blackwood
  • 4,504
  • 16
  • 32
  • 41
High Zedd
  • 53
  • 10