Introduction

Arduino IDE is capable doing some basic arithmetic operations. Arduino have five arithmatic operators for basic mathematical operations:

OperatorsOperator SymbolExample
Addition+ a = b + 2017
Subtraction-b = 2017 - a
Multiplication*d = 2.5 * e
Division/e = d / 2.5
Remainder%f = d % 2.5

Addition

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

Serial Monitor Response: Addition of Num1 and Num2 is 18

In the above code you have defined Num1 and Num2. Also you have assigned them a value. The Sum variable is defined and no value is assigned to it, hence it contains a random number. The variable Sum is used to store the result of the addition of Num1 and Num2, and the random value will be overwritten when you assign the added value to Sum. After the statement 5 has been executed, the Sum will have a value 18.

Subtraction

The subtraction operator subtracts one number from another using the minus sign (-). Given below is an example:

Serial Monitor Response: Subtraction of Num1 and Num2 is 12

The result of this operation is 12.

Multiplication

The multiplication operator multiplies one number to another using the multiplication sign (*). Given below is an example:

Serial Monitor Response: Multiplication of Num1 and Num2 is 15

The result of this operation is 45.

Division

The division operator divides one number to another using 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.

But there is one something more you should know about division. Till now we have only used division for integers and have got an integer as result. But what if the result is not integer and is a float (number with a fraction). Given below is an example:

Serial Monitor Response: Division of Num1 and Num2 is 1

The result will be 1 because the fraction is discarded when the result is stored in the integer variable. But if we use float variable to store the number, then we get the correct division result with fraction.

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, you should use a decimal point and a zero for whole numbers, i.e. 5.0 instead of 5 on its own.

Remainder

The remainder operator finds the remainder after one number is divided by another number using 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.