Evaluare utilizator: 0 / 5

Steluță inactivăSteluță inactivăSteluță inactivăSteluță inactivăSteluță inactivă
 

In this video tutorial you will learn about arithmetic operators. To do this I created a very simple calculator application.Visual Basic has powerful arithmetic operators that perform calculations with numeric variables and literals.

{jcomments on}

Arithmetic operators in Visual Basic:
+            Addition
–           Subtraction
*            Multiplication
/            Floating-point division
\            Integer division
MOD   Modulus (remainder from integer division)
^          Exponentiation (x^y = xy)

ArithmeticOperator Code:

PublicClassfrmArithOperators

   PrivateSub btnAddition_Click(sender AsObject, e AsEventArgs) Handles btnAddition.Click

       'Declaring variable

       Dim dblResult AsDouble

       'Show "+" operator

       lblOperator.Text = "+"

       'Addition

       dblResult = CDbl(txtNum1.Text) + CDbl(txtNum2.Text)

       'Display the result

       txtResult.Text = CStr(dblResult)

   EndSub

   PrivateSub btnSubstraction_Click(sender AsObject, e AsEventArgs) Handles btnSubstraction.Click

       'Declaring variable

       Dim dblResult AsDouble

       'Show "-" operator

       lblOperator.Text = "-"

       'Substraction

       dblResult = CDbl(txtNum1.Text) - CDbl(txtNum2.Text)

       'Display the result

       txtResult.Text = CStr(dblResult)

   EndSub

   PrivateSub btnMultiplication_Click(sender AsObject, e AsEventArgs) Handles btnMultiplication.Click

       'Declaring variable

       Dim dblResult AsDouble

       'Show "*" operator

       lblOperator.Text = "*"

       'Multiplication

       dblResult = CDbl(txtNum1.Text) * CDbl(txtNum2.Text)

       'Display the result

       txtResult.Text = CStr(dblResult)

   EndSub

   PrivateSub btnFloatDivision_Click(sender AsObject, e AsEventArgs) Handles btnFloatDivision.Click

       'Declaring variable

       Dim dblResult AsDouble

       'Show "/" operator

       lblOperator.Text = "/"

       'Floating-Point Division

       dblResult = CDbl(txtNum1.Text) / CDbl(txtNum2.Text)

       'Display the result

       txtResult.Text = CStr(dblResult)

   EndSub

   PrivateSub btnIntDivision_Click(sender AsObject, e AsEventArgs) Handles btnIntDivision.Click

       'Declaring variable

       Dim intResult AsInteger

       'Show "/" operator

       lblOperator.Text = "/"

       'Integer Division

       intResult = CInt(txtNum1.Text) / CInt(txtNum2.Text)

       'Display the result

       txtResult.Text = CStr(intResult)

   EndSub

   PrivateSub btnExponentiation_Click(sender AsObject, e AsEventArgs) Handles btnExponentiation.Click

       'Declaring variable

       Dim intResult AsInteger

       'Show "^" operator

       lblOperator.Text = "^"

       'Exponentiation

       intResult = CInt(txtNum1.Text) ^ CInt(txtNum2.Text)

       'Display the result

       txtResult.Text = CStr(intResult)

   EndSub

   PrivateSub btnMod_Click(sender AsObject, e AsEventArgs) Handles btnMod.Click

       'Declaring variable

       Dim intResult AsInteger

       'Show "MOD" operator

       lblOperator.Text = "MOD"

       'Modulus

       intResult = CInt(txtNum1.Text) ModCInt(txtNum2.Text)

       'Display the result

       txtResult.Text = CStr(intResult)

   EndSub

   PrivateSub btnClear_Click(sender AsObject, e AsEventArgs) Handles btnClear.Click

       txtNum1.Clear()

       txtNum2.Clear()

       txtResult.Clear()

       lblOperator.Text = String.Empty

   EndSub

   PrivateSub btnExit_Click(sender AsObject, e AsEventArgs) Handles btnExit.Click

       Me.Close()

   EndSub

EndClass