Arithmetic Operators in Arduino

Arithmetic operators in Arduino programming are used to calculate the mathematical operations taking two or more numbers as operands.The use of operators in Arduino programming plays an important role as it defines the conditions on the basis of which the program works. There are different types of arithmetic operators that are used in Arduino programming. In this write-up the mathematical operators are briefly discussed:

  • Addition operator
  • Subtraction operator
  • Multiplication operator
  • Division operator
  • Absolute operator
  • Minimum and Maximum operator
  • Square operator
  • Square root operator
  • Modulo operator
  • Power operator

Arithmetic Operators In Arduino

The arithmetic operators are used to perform basic mathematical functions. Based on these arithmetic operators the logic for the desired program can be devised. There are eleven operators used for the mathematical calculations that are explained in this write up.


When two or more numbers are to be added the addition operator is used. When writing the code in Arduino, numbers either variable or constant are declared first with integer data type. After that use the addition “+” operator for addition. This can be further explained by the code given below:

int a = 4;
int b= 2;
const int c= 1;
int add;
add= a+b+c;


The difference between any two or more values can be calculated in Arduino programming using the subtract operator “-”. If the two or more numbers are to be subtracted, they have to be declared first either constant or variable then these values can be subtracted using the subtraction operator. For better understanding the simple statements are given below:

int a = 4;
int b= 2;
int subtract;
subtract= a-b;


In Arduino programming the multiplication of two constants and variables can be done using the asterisk “*” symbol. Similarly, one constant and one variable can also be multiplied using the same method.

int a = 4;
int b = 2;
const int c= 1;
int multiply;
multiply= a*b*c;


To divide any two of the constant values and variable values, a forward slash “/” symbol is used. The variable type used for the divide operator is float so that any non-integer output can be accepted Furthermore, like the other operators one constant and another variable can also be divided:

int a = 4;
int b = 2;
Float divide;
divide= a/b;


In Arduino programming to convert a negative value into a positive value, the absolute value of that value is taken whether the value is a variable or constant. The significance of absolute is to tell how far a number is from 0 without signifying the direction; to take absolute using the Arduino code the abs command is used as illustrated in the statements below:

int c =-16;
int result;
result =abs(c);

Here in example code it can be seen that value c is 16 values away from zero.

Maximum and Minimum

The maximum and the minimum value between any two values can be found by using max() and min() functions in the Arduino program. The values can be either variables or constants:

//For maximum
int a = 4;
int b= 2;
int max_output;
max_output= max(a,b);
//For minimum
int a = 4;
int b= 2;
int min_output;
min_output = min(a,b);

From the above code the output for the maximum function will be 4 and for minimum function it will be 2 as four is greater than 2.

Square root

To take a square root of any variable or constant value the function sqrt() is used in arduino.Further it can be explained by the given example code. The square root of 100 will be 10:

int y = 100;
int = result;
result = sqrt(y);


The function used for taking the square of variable and constant is sq(). Similarly, the data types used for the operator square are float, int, double. Here in the example the square for 2.8 will be 7.84:

float f = 2.8;
float  = result;
result = sq(f);


If two values are divided and they are not divided completely as a result a residue value is left so to find that value remainder operator is used by using a percentage symbol “%”. Since in the given example both numbers are completely divisible so the remainder will be zero:

int a= 4;
int b = 2;
float result;
result = (a%b);

Power Function

This operator can be used to calculate the value of the variable or constant having the exponential form. The function used for this is pow(). To give a better understanding of the operator the pseudo code is written below. In the example 4 to raise power 2 is calculated using the pow() function nas the output will be 16.

int a = 4 ;
int b= 2;
int result;
result =pow(a,b);

Example Code

The arithmetic operators explained above are compiled together in a single program. Only for the division operator the float type variable is used and for the rest of the operators have integer type variables because division operator can have results in decimals.

void setup() {
  // put your setup code here, to run once:
  int a = 4;
  int b = 2;
  int x=-16;
  int y= 100;
  float f = 2.8;
  int result;
  float result_fl;
  Serial.print("Addition (a + b): ");
  result = a + b;
  Serial.print("Subtraction (a - b): ");
  result = a - b;
  Serial.print("Multiplication (a * b): ");
  result = a * b;
  Serial.print("Division (a / b): ");
  result_fl = a / b;
  Serial.print("Remainder (a % b): ");
  result = a % b;
  Serial.print("absolute of -16 is: ");
  Serial.print("maximum value is: ");
  Serial.println(max(a, b));
  Serial.print("minimum value is: ");
  Serial.println(min(a, b));
  Serial.print("square of 2.8 is: ");
  Serial.print("value for 4^2 is: ");
  result=pow(a, b);
  Serial.print("square root of 100 is:");
void loop() {
  // put your main code here, to run repeatedly:



Arithmetic operators in Arduino programming are helpful in determining the basic logic behind which a code runs. This write-up explains what arithmetic operators are and how they can be used for mathematical calculations which can be used to make conditions for any specific task to be performed.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.