Fundamentals of Programming: Arithmetic operations

From Wikibooks, open books for an open world
Jump to: navigation, search

PAPER 1 - ⇑ Fundamentals of programming ⇑

← Iteration Arithmetic operations Boolean operations →

Arithmetic Operation - is a function which can perform one of the following tasks: adding, subtracting, multiplying and dividing


Operator - a programming device that performs a function on one or more inputs, for example arithmetic operators (+,-,/,*)


Programming languages provide a basic set of operators to calculate simple arithmetic.

+   Addition
-   Subtraction
*   Multiplication
/   Division
\   Integer division
Mod Remainder Division
^   Exponentiation
&   String concatenation

7 + 2     produces 9
7 - 2     produces 5
7 * 2     produces 14
7 / 2     produces 3.5
7 \ 2     produces 3
7 Mod 2   produces 1
7 ^ 2     produces 49
"7" & "7" produces "77"

Let's look at a short example of arithmetic operations before we jump into the operators themselves.

In this example we will also be using some basic variables. In VB.NET the Dim operator creates the variable, whilst in Python you can simply assign the value to the variable.

VB.NET Python
1  Dim Commission As Single
2  Dim Sales As Single
3  Sales = 3142.51
4  Commission = 0.3 * Sales  ' Calculate 30% commission.
1  Sales = 3142.51
2  Commission = 0.3 * Sales  # Calculate 30% commission.


First, we set the total Sales to 3142.51.

The * operator calculates multiplication, so line 4 is equivalent to multiplying 0.3 and Sales together. Sales is 3142.51, so our result should be the product of 0.3 and 3142.51, and stored in Commission.

Why the funny symbols?[edit]

With the exception of addition and subtraction, the symbols used are different to the ones used in real life. This is simply because the other symbols are not available on a standard keyboard (try and find ÷ ≠ m² on yours!) or the symbol is in the alphabet and can be used for naming a variable (x).

Addition[edit]

This adds two numbers together, and is denoted by the "+" symbol. If strings are involved it may also do String concatenation, that means sticking the two strings together. Examples:

VB.NET Python
1  x = 7 + 2     ' Results in 9.
2  x = 25 + -4   ' Results in 21.
3  Dim StringA As String
4  StringA = "A string" + "Another string" ' Results in "A stringAnother string"
1  x = 7 + 2     # Results in 9.
2  x = 25 + -4   # Results in 21.
3  
4  StringA = "A string" + "Another string" # Results in "A stringAnother string"

There is a second addition operator, "+=". It increments the variable on the left of the += by the amount indicated on the right. Examples:

VB.NET Python
1  Dim x As Integer = 54
2  x += 89       ' result is 143
3  x += 7       ' result is 150
1  x = 54
2  x += 89       # result is 143
3  x += 7       # result is 150

It also works with Strings as a concatenation operator. Examples:

VB.NET Python
1  Dim x As String = "A fox"
2  x += " jumped"          ' result is "A fox jumped"
3  x += " over the fence"  ' result is "A fox jumped over the fence"
1  x = "A fox"
2  x += " jumped"          # result is "A fox jumped"
3  x += " over the fence"  # result is "A fox jumped over the fence"

Subtraction[edit]

This subtracts two numbers, and is denoted by the "-" symbol. Examples:

VB.NET Python
1  Dim x As Integer
2  x = 7 - 2    ' Results in 5.
3  x = 25 - -4  ' Results in 29.
1  
2  x = 7 - 2    # Results in 5.
3  x = 25 - -4  # Results in 29.

Multiplication[edit]

This multiplies two numbers, and is denoted by the "*" symbol. Examples:

VB.NET Python
1  Dim x As Integer
2  x = 7 * 2    ' Results in 14.
3  x = 25 * -4  ' Results in -100.
1  
2  x = 7 * 2    # Results in 14.
3  x = 25 * -4  # Results in -100.

Division[edit]

There are more types of division than the one denoted by the "/" symbol. There is also integer division and remainder division.

Normal[edit]

This is the most commonly used form of division and is denoted by the "/" operator. Examples:

VB.NET Python
1   Dim x As Single
2  ' (note that we must use the Single class to have decimals)
3  x = 7 / 2  ' Results in 3.5.
4  x = 25 / 4 ' Results in 6.25
1  
2  
3  x = 7 / 2  # Results in 3.5.
4  x = 25 / 4 # Results in 6.25

Integer division[edit]

This divides two numbers, and gives the result without the remainder if the quotient is a decimal. Examples:

VB.NET Python
1   Dim x As Integer
2  x = 7 \ 2    ' Results in 3.
3  x = 25 \ 4   ' Results in 6.
1  x = 7 \ 2    # Results in 3.
2  x = 25 \ 4   # Results in 6.

Remainder Division[edit]

This divides two numbers, and gives the result's remainder if the quotient is a decimal. This is denoted by the operator "Mod" in VB.NET and "mod" in Python. Examples:

VB.NET Python
1  Dim x As Integer
2  x = 7 Mod 2  ' Results in 1.
3  x = 25 Mod 4 ' Results in 1.
1  
2  x = 7 mod 2  # Results in 1.
3  x = 25 mod 4 # Results in 1.

Exponentiation[edit]

This is raising a number to a power, i.e. is 49 . For example is:

VB.NET Python
1  Dim x As Integer
2  x = 7 ^ 2   ' Results in 49.
1  
2  x = 7 ^ 2   # Results in 49.

This results in the number 49 being assigned to the variable x. It can also be used to calculate the square root of a number. The square root of a number is the number raised to the power of 0.5.

VB.NET Python
1  Dim x As Integer
2  x = 7 ^ 0.5   ' Results in 2.645.
1  
2  x = 7 ^ 0.5   # Results in 2.465.

Note: It is necessary to ensure that the variables be correctly declared to get the desired results. The following example works, but will produce the wrong result. This is because the Integer class does not allow decimal places (just like mathematical integers.)

VB.NET Python
1  Dim x As Integer
2  x = 9 ^ 0.5   ' Results in 3.
1  
2  x = 9 ^ 0.5   # Results in 3.

Since x is declared as an Integer type, the value square root, a real number, is stored incorrectly.

Any nth root of number can be calculated by raising the number to the power of :

VB.NET Python
1  Dim x As Single
2  Dim n As Single
3  n = 7
4  x = 2 ^ (1 / n)
1  n = 7
2  x = 2 ^ (1 / n)

This is because .

BODMAS[edit]

You have probably learnt about the order of operations in maths. BODMAS also applies to computer calculations. This means that when calculating a sum, the program will calculate:

  1. Brackets
  2. Order (powers n^2 etc)
  3. Division
  4. Multiplication
  5. Addition
  6. Subtraction
Example:BODMAS in vb

For example:

console.writeline( (3+4)/7 )
console.writeline( 3+4/7 )
console.writeline( 3+4/7-1 )
  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

1
3.57142857
2.57142857


If in doubt use the brackets!