Donate to help Starting Electronics using the **Donate** button below. Any amount appreciated.

Created on: 25 September 2014

**Part 4 of the Arduino Programming Course**

The Arduino can do mathematics for us. In this part of the course, we look at how to do addition, subtraction, multiplication, division, and find a remainder.

Below, five arithmetic operators are described and then all put into a sketch to demonstrate how they work on the Arduino.

To add numbers on the Arduino, we use the addition operator (**+**).

The example below shows how to add two numbers together.

int a = 2; int b = 7; int sum; sum = a + b;

In the above code, three variables are defined. Variables **a** and **b** are each assigned a value when they are defined.

The **sum** variable is defined, but not initialized, so contains any random number. We will use this variable to store the result of the addition calculation, so the random value that **sum** contains will be overwritten when we put the addition result (or sum) into it.

After the statement shown below has been executed, **sum** will contain the value **9** i.e. the result of the addition of variable **a** and **b**.

sum = a + b;

We can also add two constant values and store the result in a variable as shown below.

```
int sum;
sum = 2 + 10;
```

The result stored in **sum** after execution of the addition statement will be **12** in this example.

Constant values and variables can also be added together and the result stored in a variable as shown here.

int a = 3; int sum; sum = a + 24;

After execution of the addition, **sum** will contain **27**.

The remaining arithmetic operators can also operate on constant values, variables and a mixture of both.

The subtraction operator subtracts one number from another using the minus sign (**-**) as the following example shows.

```
int result;
result = 10 - 2;
```

The result of this calculation will be **8** and will be stored in the **result** variable.

Multiplication is done by using the multiplication operator (*****).

```
int result;
result = 4 * 3;
```

The result of the above calculation will be **12** and will be stored in the **result** variable.

The division operator (**/**) is used to perform division in the Arduino.

```
int result;
result = 12 / 3;
```

The result of the above calculation will be 4.

So far we have only been using integer values to perform arithmetic. If the result of a division is not an integer (or whole number), but contains a fraction part, the fraction part will be discarded if the result is stored in an integer variable.

The following examples will demonstrate what happens when a result with a fractional part is stored in an integer and then a floating point variable.

```
int result;
result = 5 / 4;
```

The result will be **1** because the fraction is discarded when the result is stored in the integer variable **result**.

The same calculation, but this time defining **result** as a floating point variable (**float**).

```
float result;
result = 5.0 / 4.0;
```

The result now contained in the **result** variable will be **1.25**.

When using constant values in calculations that store the result in a floating point variable, we use a decimal point and a zero for whole numbers, i.e. 5.0 instead of 5 on its own.

The remainder operator (or modulo operator) is used to find the remainder after the division of two numbers. The percentage sign (**%**) is used as the modulo operator.

```
int result;
result = 11 % 4;
```

The result of this calculation will be the remainder of 11 divided by 4 which is **3** (4 goes into 11 twice leaving a remainder of 3).

The following sketch demonstrates all of the above arithmetic operators.

Load the sketch to your Arduino and try it out. Also add the examples from above that are not in the sketch, such as mixing constant values and variables.

voidsetup() { int a = 2; int b = 7; int result; float result_fl;Serial.begin(9600);Serial.print("Addition (a + b): "); result = a + b;Serial.println(result);Serial.print("Subtraction (10 - 2): "); result = 10 - 2;Serial.println(result);Serial.print("Multiplication (4 * 3): "); result = 4 * 3;Serial.println(result);Serial.print("Int Division (5 / 4): "); result = 5 / 4;Serial.println(result);Serial.print("Float Division (5.0 / 4.0): "); result_fl = 5.0 / 4.0;Serial.println(result_fl);Serial.print("Remainder (11 % 4): "); result = 11 % 4;Serial.println(result); } voidloop() { }

This video shows the above sketch running.

Can't see the video? View on YouTube →

The sketch contains a mix of **print()** and **println()** functions. print() is called when the invisible cursor must stay on the same line so text printed in the println() statement that follows will be printed on the same line as well.

println() is used when text must be printed and then the invisible cursor moved to the next line so that the next print() statement will print text on a new line.

A separate **result_fl** variable was defined to hold the result of the floating point calculation.

The integer variable **result** is reused many times during the sketch. The important thing is to display its result before placing the result of the next calculation into it.

We looked at variables in the last part of this course. **Constants** are numbers that remain constant throughout a sketch – i.e. they never change.

Examples of constants are the numbers that are used directly in a calculation and are not assigned to a variable, e.g. sum = 2 + 3 contains the constants 2 and 3.

Constants do not have a name and their values can not be changed when the sketch is running.

Although not shown in the above examples, addition, subtraction and multiplication can also be done on floating point values.

To comment on this article or ask questions, go to the blog entry for this article. Use the comment box at the bottom of the blog entry.

© 2012 – 2020, Starting Electronics