Pages

Monday, 21 March 2016

QUADRATIC EQUATION ROOT CALCULATOR WITH COMPLEX ROOT ON VISUAL BASIC



QUADRATIC EQUATION ROOT CALCULATOR WITH COMPLEX ROOT ON VISUAL BASIC


Hello guys, Iits been a while, I recently wrote a program in visual basic which calculates the root of a quadratic equation and think I should share it with you guys.
Ok, here’s the code……..


Module Module1

    Sub Main()
        'This is a comment
       Console.WriteLine("This is a Quadratic Calculator")
        'Declaring variables
        Console.WriteLine("Input a: ")
        Dim a As Integer
        a = Integer.Parse(Console.ReadLine())
        Console.WriteLine("Input b: ")
        Dim b As Integer
        b = Integer.Parse(Console.ReadLine())
        Console.WriteLine("Input c: ")
        Dim c As Integer
        c = Integer.Parse(Console.ReadLine())
        Dim d As Double
        Dim e As Double
        Dim x1 As Double
        Dim x2 As Double
        'getting the value in the square root
        e = Math.Pow(b, 2) - (4 * a * c)
        'checking for equal root, different roots and complex roots
        If e > 0 Then
            d = Math.Sqrt(e)
            x1 = (-b + e) / (2 * a)
            x2 = (-b - e) / (2 * a)
            Console.WriteLine("Equation roots: x1 = {0}, \nx2 = {1}", x1, x2)
        ElseIf e = 0 Then
            x1 = -b / 2
            x2 = x1
            Console.WriteLine("Equation roots: x1 = {0}, \nx2 = {1}", x1, x2)
        Else : Console.WriteLine("Equation has complex roots")
            d = Math.Round(Math.Sqrt(-1 * e) / 2, 2)
            x1 = -b / 2
            Console.WriteLine("Roots: {0} + i{1} and {0} - i{1}", x1, d)
        End If
        Console.WriteLine("Thanks")

        Console.ReadLine()
    End Sub

End Module


Code explained in the video below.



No comments:

Post a Comment