Arduino IDE: Arithmetic Operators

About This Tutorial
This tutorial discusses what are arithmetic operators, and the arithmetic operators in Arduino IDE for addition, subtraction, multiplication, division, and finding out the remainder.
Tutorial Info
Programming Platform:
Difficulty:
Estimated Time (hrs):
1

Introduction

Arduino IDE is capable doing basic arithmetic operations. Arduino has the following five arithmetic 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

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 the statement 5 is executed, the Sum will have a value 18.

Subtraction

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.

Multiplication

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.

Division

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 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 is 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

evive Tips and Tricks
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.

Remainder

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

This Helped
This didn't help
Close Menu
×

Cart

Hey! we are providing FREE online course on
Arduino Uno R3 ATL