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"
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. If given the values 9 & 3 in that order, which operator would give the value 3?
2. If given the values 9 & 3 in that order, which operator would give the value 12?
3. If given the values 3 & 9 in that order, which operator would give the value 6?
4. Select the correct output be for the operation below:

10 MOD 2
5. What is the difference between the / and // operators?

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!

Presentation

Coming Soon!

Students

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

Sign Up For Membership Today

Student Subscription

Online revision resources for individual students
£ 2
50
Monthly
  • Over 100 Topic Introductions
  • Additional topic info
  • Download Revision Resources
  • Quizzes
  • Flash Cards
Student

School Subscription

Online revision resources for whole schools
£ 12
50
Monthly
  • Additional topic info
  • Download Revision Resources
  • Quizzes
  • Flash Cards
  • Lesson Plans
  • Lesson Presentations
  • 50 student accounts included
School

Scribbl.it Notes

The Arithmetic Operations Scribbl.it 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 Scribbl.it Notes (whether structured like these, or independently created):

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

Scroll to the bottom to download

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

10 MOD 2
5. If given the values 3 & 9 in that order, which operator would give the value 6?