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).

Calculator maths

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"
OUTPUT "Number is odd"

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. What is the difference between the / and // operators?
2. If given the values 9 & 3 in that order, which operator would give the value 3?
3. Select the correct output be for the operation below:

10 MOD 2
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 12?

<< Iteration

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!

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit

Looking For More?

Lesson Plan

Coming Soon!


Coming Soon!


Click to revise!

Not a  member yet? Sign Up Here

Or log in below to view resources on this page

Sign Up For Membership Today

Individual Student

Individual Site License
Topic Introduction Pages
Additional Revision Resources
Revision Videos
Exam Question Walkthroughs
Discounted Group Lessons
£2.50 per Month. After your initial payment, your first payment is Free.

Whole School

Whole School Site License (teacher access)
Up to 50 student accounts (£3 per student, per annum for additional students)
Online Topic Lesson Plans
Differentiated Homework Tasks
Monthly Bundle of Downloadable Resources
Discounted Live Lessons
£12.50 per Month.
Number of courses 30
Getting Started with LESS
Improve Your CSS Workflow with SASS
Easy Zurb Foundation