### Arithmetic Operations in Programming

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

• –    (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 = INPUTOUTPUT "Please enter the second letter"endLetter = INPUTdistance = 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 2remainder = 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 = INPUTIF myInt MOD 2 == 0 THEN   OUTPUT "Number is even"ELSE   OUTPUT "Number is odd"END IF

Try this for yourself, use the quiz below to test your knowledge, or use the Student revision area for more practical examples.

Test your knowledge with the Arithmetic Operations Quiz!

1. Select the correct output be for the operation below:

10 MOD 2
2. What is the difference between the / and // operators?
3. If given the values 9 & 3 in that order, which operator would give the value 12?
4. If given the values 3 & 9 in that order, which operator would give the value 6?
5. If given the values 9 & 3 in that order, which operator would give the value 3?

## Looking For More?

Coming Soon!

Coming Soon!

Click to revise!

### Student Subscription

Online revision resources for individual students
£ 2
50
Monthly
• Over 100 Topic Introductions
• Quizzes
• Flash Cards
Student

### School Subscription

Online revision resources for whole schools
£ 12
50
Monthly
• Quizzes
• Flash Cards
• Lesson Plans
• Lesson Presentations
• 50 student accounts included
School

The Arithmetic Operations Doodle Notes provide a structured way for students to revise topic areas covered in class in a visual way. Encourage students to colour in areas of importance, add doodles and colour, then add more detail to the notes page being as creative as possible.

There are known benefits to using Doodle Notes (whether structured like these, or independently created):

• Increased Attention
• Reduction of stress levels
• Increased memory for content
• Deeper learning

10 MOD 2