Arithmetic Operators
Basic Operators
+ Add
- Subtract
* Multiply
/ Divide
NB: When storing the result of a division you must choose an appropriate data type.
Advanced Operators
Integer division tell you how many times the divisor fits completely into the dividend.
Modulo will give you the reminder after a division.
Think back to when you did maths at primary school, before you learnt about the decimal place....
When you were asked to do a calculation like 14 ➗3, you may have said the answer was 4 remainder 2.
The 4 in the example above is the result of an integer division - how many times 3 completely fits into 14.
In Pseudocode we use the keywords:
DIV - integer division
MOD - The remainder of an integer division
The answers will always be integers.
Example
A taxi can only hold 7 passengers.
How many taxis are needed to drive 17 people to the airport?
If we divide 17 by 7 we would get 2.43, but you can't have 2.43 taxis.
If we do an integer division then we would get 2. You can have 2 taxis.
To discover if we need an additional taxi we would MOD 17 by 7 to see if there was a remainder. If there is, we would need an extra taxi.
The pseudo code might look like this:
CONSTANT maxPassengers ← 7 //INTEGER
OUTPUT("Enter the number of passengers: ")
INPUT passengers //INTEGER
numOfTaxis = passengers DIV maxPassengers //INTEGER
IF passengers MOD maxPassenger > 0 THEN
numOfTaxis = numOfTaxis + 1
ENDIF
OUTPUT("You need ", numOfTaxis, " taxis")
See example in:
VB.Net
C#