Zebra0.com

visual-basic arithmeticVisual Basic: Windows Programming

Visual Basic: Windows Programming

Learn Visual Basic in FREE step-by-step lessons.

Arithmetic Operations

Arithmetic operators

Visual Basic has the following operators:

Operator Purpose Example Result
+ addition 5+3 8
- minus 5-3.0 2.0
* multiplication 5*3 15
/ division /2 4.5
\ integer division 9\2 4
Mod remainder 14 Mod 3 2
^ exponentiation 5^2 25

An actual program would not use a statement such as n=5+3 it would save time to simply use n=8. An actual program would be more likely to use variables: n=a+b for example.

Note that there is a times, or multiplication, operator: *. In algebra, variables are always a single letter, XY in algebra means X times Y. In programming, variables can be several letters, and we could not be sure whether XY meant X times Y or a variable called XY.

Remainder: Mod

The Mod operator is used to find the remainder. Before children learn about decimal numbers, they may give the answer to division problems as: "17 divided by 5 is 3 with a remainder of 2" Note that 17/5 results in 3.4, while 17 Mod 5 results in 2. If we want to get just the integer part of a division we need to use the int function: Int(17 / 5) will result in 3.

You can test any of these operations in Visual Basic with the form load event:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text =17 \ 5
    End Sub
End Class

Please study the material at each of the links below.

  1. Operators

  2. The mod operator: remainder

  3. finding dozens

  4. Order of operations

  5. Drill: Order of operations

  6. calculate grade

  7. Algebra to code

  8. Decimal values on scroll bars

  9. Random Numbers

GlossaryGlossary for arithmetic lesson
Full Glossary