VBScript – Arithmetic Operators

All the programming or scripting languages support Operators which are used to do some action.  For example “+” is used to add two numbers as well as concatenate two strings. We might need to compare values in two variables, then use < , >, ==, etc.  These are some examples of operators.  Let us have a look into the operators supported by vbscript.

Following are the different category of operators in vbscript:

  1. Arithmetic operators
  2. Logical operators
  3. Comparison operators
  4. Concatenation operators

Arithmetic Operators:

These are used to conduct arithmetic operations such as ADD, SUBTRACT, MULTIPLY, DIVIDE, etc.

Operation VBScript Operator
Addition +
Subtraction
Multiply *
Divide /
Exponent ^
Modulus mod

Example:

Dim sum,subtract,mult,divd,res,Z,md

A=cint(inputbox("Enter value for A"))
B=cint(inputbox("Enter value for B"))

sum = A+B
subtract=B-A
mult=A*B
divd=B/A
res="Summation of A & B : "&sum&vbnewline&_
         "Subtraction of B from A : "&subtract&vbnewline&_
         "Multiplication of A & B : "&mult&vbnewline&_
         "Division of B by A : "&divd&vbnewline

 msgbox res


'To demonstrate Exponentiation

X=cint(inputbox("Enter value for X"))
Y=cint(inputbox("Enter value for Y"))

Z=X^Y
'5 raised to power 2 = 25 :  X=5, Y =2 should result in 25
msgbox "Exponentiation of Y to X : "&Z

'To demonstrate Modulus
'if X=11 and Y=2 then answer should be 1 - X is divided by Y then the remainder is the result
X=cint(inputbox("Enter value for X"))
Y=cint(inputbox("Enter value for Y"))

md= X mod Y

msgbox "Modulus of X, Y : "&md

The above example demonstrates many things including arithmetic operators, built-in functions cint, inputbox, and msgbox.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.