Lesson Progress

0% Complete

Arduino IDE is capable of doing basic arithmetic operations. Arduino has the following five arithmetic operators for basic mathematical operations:

Operators | Operator Symbol | Example |
---|---|---|

Addition | + | a = b + 2017 |

Subtraction | - | b = 2017 - a |

Multiplication | * | d = 2.5 * e |

Division | / | e = d / 2.5 |

Remainder | % | f = d % 2.5 |

The addition operator is used for adding two numbers. Here is an example:

**Serial Monitor Response:** `Addition of Num1 and Num2 is 18`

In the code above, two variables are defined, namely **Num1** and **Num2**. A value is assigned both of them as well. The third variable, i.e. **Sum** is defined and no value is assigned to it; hence, it contains a random number. This variable is used to store the *sum* of **Num1** and **Num2**; this value will overwrite the random value when you assign it to Sum. After statement 5 is executed, the Sum will have a value *18*.

The subtraction operator subtracts one number from another. It is represented by the plus sign (+). Given below is an example:

**Serial Monitor Response:** `Subtraction of Num1 and Num2 is 12`

The result of this operation is *12*.

The multiplication operator multiplies one number with another. It is represented by the asterisk (*). Given below is an example:

**Serial Monitor Response:** `Multiplication of Num1 and Num2 is 15`

The result of this operation is *45*.

The division operator divides one number by another. It is represented by the division sign (/). Given below is an example:

**Serial Monitor Response:** `Division of Num1 and Num2 is 5`

The result of this operation is *5*.

There is, however, one more thing you should know about division. Till now we have used division only for integers and have got an integer as a result. But what if the result is a floating-point number (numbers with a decimal) and not an integer? Let’s find out with the help of the example given below:

**Serial Monitor Response:** `Division of Num1 and Num2 is 1`

The result will be 1 because the numbers after the decimal point are discarded when the result is stored in the variable because its data type is **int**. However, if we use float as the data type of the variable to store the result, we get the correct result.

**Serial Monitor Response:** `Division of Num1 and Num2 is 1.20`

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

The remainder operator calculates the remainder after one number is divided by another number. It is represented by the percentage sign (%). Given below is an example:

**Serial Monitor Response:** `Remainder of Num1 and Num2 is 1`

The result of this operation is *1*.

That’s all for arithmetic operators. We will learn about relational operators in the next topic.

Quizzes