ARTICLE

Quadratic Equation Solver

Posted by Deepak Kumar Verma Articles | Visual Basic 2010 February 09, 2011
Quadratic Equation Solver is a small application to find out the root(s) of the quadratic equation rapidly.
 
Reader Level:

Quadratic Equation Solver is a small application which allows you to solve a quadratic equation and produce the result very fast and correct.

Here is the source code of the application "Quadratic Equation Solver"

Code :

  • Module1

Module Module1

     Public a, b, c, det, r1, r2 As Single

End Module

  • Form1 

Imports System.Console

Imports System.Math

Public Class Form1

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

        Texta.Text = ""

        Textb.Text = ""

        Textc.Text = ""

        Textr1.Text = ""

        Textr2.Text = ""

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QESExit.Click

        End

    End Sub

    Private Sub Solve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Solve.Click

        Textr1.Text = ""

        Textr2.Text = ""

        If Texta.Text = "0" Then

            MsgBox("Cofficient a must not be 0 !!!")

            Texta.Text = ""

            Texta.Focus()

        ElseIf Texta.Text = "" Or IsNothing(Texta.Text) Or IsNumeric(Texta.Text) = False Then

            MsgBox("Cofficient a must have an numberic value !!!")

            Texta.Text = ""

            Texta.Focus()

 

        ElseIf Textb.Text = "" Or IsNothing(Textb.Text) Or IsNumeric(Textb.Text) = False Then

            MsgBox("Cofficient b must have an numberic value !!!")

            Textb.Text = ""

            Textb.Focus()

 

        ElseIf Textc.Text = "" Or IsNothing(Textc.Text) Or IsNumeric(Textc.Text) = False Then

            MsgBox("Cofficient a must have an numberic value !!!")

            Textc.Text = ""

            Textc.Focus()

        Else

            a = Texta.Text

            b = Textb.Text

            c = Textc.Text

            det = (b * b) - (4 * a * c)

            If (det > 0) Then

                r1 = (-b + Sqrt(det)) / (2 * a)

                r2 = (-b - Sqrt(det)) / (2 * a)

                Answer.Text = "There are two roots :: "

                Textr1.Enabled = True

                Textr2.Enabled = True

                Textr1.Text = r1

                Textr2.Text = r2

            ElseIf (det = 0) Then

                r1 = (-b + Sqrt(det)) / (2 * a)

                Answer.Text = "There is only one root :: "

                Textr1.Enabled = True

                Textr1.Text = r1

            Else

                Answer.Text = "There is no root !!!"

            End If

        End If

    End Sub

    Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reset.Click

        Texta.Text = ""

        Textb.Text = ""

        Textc.Text = ""

        Textr1.Text = ""

        Textr2.Text = ""

        Answer.Text = ""

        Textr1.Enabled = False

        Textr2.Enabled = False

        Texta.Focus()

    End Sub

    Private Sub Texta_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Texta.GotFocus

        Texta.SelectAll()

    End Sub

    Private Sub Textb_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Textb.GotFocus

        Textb.SelectAll()

    End Sub

    Private Sub Textc_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Textc.GotFocus

        Textc.SelectAll()

    End Sub

End Class

 

Output :

This is the simple view of the application

QES_1.gif

When you leave any text box as blank you will see an message box as shown in below figure

QES_2.gif

When you assign a 0 to "coefficient a" You will receive an message box as shown in below figure

QES_3.gif

Solution in case of two roots found

QES_4.gif

Solution in case of one root found

QES_5.gif

Solution in case of no root found

QES_6.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor