make a simple calculator involving the following operations addition, subtraction, multiplication, division, square-root and percentage!!thanks....:)

Dani AI

Generated

Building on 's request and the quick UI tip from (two inputs + a result) while keeping 's point about math functions in mind: here is a compact, practical pattern for a VB.NET Windows Forms calculator that implements addition, subtraction, multiplication, division, square root and percentage, with input validation and edge-case handling.

Use two TextBoxes (txtA, txtB), a Label (lblResult) and operator Buttons (btnAdd, btnSub, btnMul, btnDiv, btnSqrt, btnPercent). Percent behavior implemented below: when both inputs exist it computes A% of B ((A/100)*B); when only A is present it returns A/100. Always validate with Double.TryParse and check divide-by-zero and negative input for square root; see Double.TryParse and Math.Sqrt for details.

Private Function TryGetValues(ByRef a As Double, ByRef b As Double) As Boolean
    If Not Double.TryParse(txtA.Text, a) Then
        MessageBox.Show("Enter a valid number for A")
        Return False
    End If
    Double.TryParse(txtB.Text, b) ' b optional
    Return True
End Function

Private Sub btnDiv_Click(sender As Object, e As EventArgs) Handles btnDiv.Click
    Dim a,b As Double
    If Not TryGetValues(a,b) Then Return
    If b = 0 Then
        lblResult.Text = "Cannot divide by zero"
        Return
    End If
    lblResult.Text = (a / b).ToString()
End Sub

Private Sub btnSqrt_Click(sender As Object, e As EventArgs) Handles btnSqrt.Click
    Dim a As Double
    If Not Double.TryParse(txtA.Text, a) Then Return
    If a < 0 Then lblResult.Text = "Invalid input (negative)" : Return
    lblResult.Text = Math.Sqrt(a).ToString()
End Sub

Private Sub btnPercent_Click(sender As Object, e As EventArgs) Handles btnPercent.Click
    Dim a,b As Double
    If Not TryGetValues(a,b) Then Return
    If txtB.Text.Trim() = "" Then lblResult.Text = (a / 100).ToString() Else lblResult.Text = ((a / 100) * b).ToString()
End Sub

Quick tips: format results with Math.Round for display, handle culture-specific decimals if needed, and prefer Double (or Decimal for financial accuracy). This pattern keeps the UI simple as suggested but adds the validations and behaviors students will commonly expect.

Recommended Answers

All 2 Replies

Calculator is helpul every student life.It is using all the mathematical functions.
============================
jack008

just draw two Textbox as input and one Label as result and a Button to process it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.