Click to see the rest of the programming section :

Programmers make use of arithmetic operations in programming for a variety of reasons. Most of these operations will already be familiar to you from your Maths lessons and can be applied to your programs by knowing the syntax (spelling & grammar of a language).

Arithmetic operators in programming refer to the mathematical operators in the language. These are

- +
*(add)* - –
*(subtract)* - /
*(divide)* - *
*(multiply)* - DIV
*(integer division)* - MOD
*(remainder after integer division)*

Whilst some programs use obvious maths, such as outputting a times table program, others may have less obvious maths built in.

Consider program that needs to calculate the distance between two letters in the alphabet that the user has entered. Initially, this looks like it is a program that is only using string & char data types. But, in fact we are looking at the position (or index) of a letter in the alphabet.

In this example we would need to save a start and end variable and use an arithmetic comparrison to output the distance between the tow letterse. In pseudocode, this may look like:

alphabet = "abcdefghijklmnopqrstuvwxyz"

user = ""

OUTPUT "Please enter the first letter"

startLetter = INPUT

OUTPUT "Please enter the second letter"

endLetter = INPUT

distance = alphabet.index(endLetter) - alphabet.index(startLetter)

OUTPUT startLetter + "is" + distance + "places from" + endLette

If we were to translate this into Python 3, this would look like the code below. Try extending this code in the browser to output whether the start letter is before or after the end letter by checking for a negative number (*press Ctrl + Enter to run the code*):

The final two operators in this section may not be as familiar to you at first, but they are actually something that you have been using since primary school!

**DIV** is used to create a calculation for integer division. Put simply, this will output how many times one number will go into another. eg.

10 DIV 2 = 5

Why? Because 2 goes into 10 5 times.

**MOD** is very similar, but instead of calculating the integer division, MOD gives the remainder after the division is completed. So, if we combined the two, we could have:

*Hover over the image for the answer!*

division = 10 DIV 2

remainder = 10 MOD 2

DIV is particularly useful in programs where we don’t want a real number returned, or we want to round down.

MOD is often used to check if one number is divisible by another. For example, we could write a program to check if a number is even using the following pseudocode:

OUTPUT "Enter an integer"

myInt = INPUT

IF myInt MOD 2 == 0 THEN

OUTPUT "Number is even"

ELSE

OUTPUT "Number is odd"

END IF

Try this for yourself, or use the Student revision area for more practical examples.

*Find this page helpful? Share the love on your social media mentioning @TeachAllAboutIT and we’ll enter you in our monthly draw to win a gift voucher for any product on the site!*

## Looking For More?

Download links are visible for logged in subscribers.

Not a student member yet? Sign Up Here from £2.50 per month

Or purchase these resources individually from our resources