# Arduino IDE: Arithmatic Operators

**Introduction**

Arduino IDE is capable doing some basic arithmetic operations. Arduino have five arithmatic 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 |

**Addition**

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

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int Num1 = 15; int Num2 = 3; int Sum; void setup(){ Serial.begin(9600); Sum = Num1 + Num2; Serial.print("Addition of Num1 and Num2 is "); Serial.println(Sum); } void loop() { } |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int Num1 = 15; int Num2 = 3; int Result; void setup(){ Serial.begin(9600); Result = Num1 - Num2; Serial.print("Subtraction of Num1 and Num2 is "); Serial.println(Result); } void loop() { } |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int Num1 = 15; int Num2 = 3; int Result; void setup(){ Serial.begin(9600); Result = Num1 * Num2; Serial.print("Multiplication of Num1 and Num2 is "); Serial.println(Result); } void loop() { } |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int Num1 = 15; int Num2 = 3; int Result; void setup(){ Serial.begin(9600); Result = Num1 / Num2; Serial.print("Division of Num1 and Num2 is "); Serial.println(Result); } void loop() { } |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int Num1 = 6; int Num2 = 5; int Result; void setup(){ Serial.begin(9600); Result = Num1 / Num2; Serial.print("Division of Num1 and Num2 is "); Serial.println(Result); } void loop() { } |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int Num1 = 6; int Num2 = 5; float Result; void setup(){ Serial.begin(9600); Result = (float) Num1 / Num2; Serial.print("Division of Num1 and Num2 is "); Serial.println(Result); } void loop() { } |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
int Num1 = 6; int Num2 = 5; int Result; void setup(){ Serial.begin(9600); Result = Num1 % Num2; Serial.print("Remainder of Num1 and Num2 is "); Serial.println(Result); } void loop() { } |

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

The result of this operation is 1.