Arduino arithmetic operators

Share

After learning about data types, variables, and constants in Arduino programming, the next step is understanding what arithmetic operators are and how they are used in Arduino code.

Arithmetic operators perform arithmetic operations between two or more numbers or operands. It plays a crucial role in Arduino programming as it defines the conditions based on which the program works.

Arithmetic operators in Arduino programming

The arithmetic operators are used to perform essential mathematical functions. Based on these arithmetic operators, the logic for the desired program can be devised. Eleven operators are used for the mathematical calculations explained in this write-up.

The operators include:

  • Addition operator

  • Subtraction operator

  • Multiplication operator

  • Division operator

  • Square operator

  • Square root operator

  • Modulo operator

  • Power operator

  • Absolute operator

  • Minimum and Maximum operator

Addition operator

The addition operator performs the addition operation when two or more numbers are supposed to be added. The operands on which the addition operation is performed are declared as an integer and are initialized to an integer value. For addition the + operator is used.

Addition operator
Addition operator
int c;
int a = 6;
int b = 3 ;
void setup ( )
{
Serial.begin( 9600 );
c = a + b;
Serial.println(c);
}
void loop ( )
{
//your code here
}
// output is 9
Implementation of addition operator

Subtraction operator

The subtraction operator performs the subtraction operation when two or more numbers are supposed to be subtracted. The operands on which the subtraction operation is performed are declared integers and initialized to an integer value. For subtraction the - operator is used.

Subtraction operator
Subtraction operator
int c;
int a = 6;
int b = 3 ;
void setup ( )
{
Serial.begin( 9600 );
c = a - b;
Serial.println(c);
}
void loop ( )
{
//your code here
}
// output is 3
Implementation of subtraction operator

Multiplication operator

The multiplication operator performs the multiplication operation when two or more numbers are supposed to be multiplied. The operands whose product is to be calculated or on which multiplication operation is performed are declared integers and initialized to an integer value. For multiplication the * operator is used.

Multiplication operator
Multiplication operator
int c;
int a = 6;
int b = 3 ;
void setup ( )
{
Serial.begin( 9600 );
c = a * b;
Serial.println(c);
}
void loop ( )
{
//your code here
}
// output is 18
Implementation of multiplication operator

Division operator

The division operator is used to divide a number by another. The operands to be divided are declared integers and initialized to an integer value, whereas the variable that will store the division value is declared as a float value to store the non-integer value. For division the / operator is used.

Division operator
Division operator
float c;
int a = 6;
int b = 3 ;
void setup ( )
{
Serial.begin( 9600 );
c = a / b;
Serial.println(c);
}
void loop ( )
{
//your code here
}
// output is 2.00
Implementation of division operator

Square operator

The square operator is used to calculate the square of a number. The square is calculated of a variable or a constant value. For calculating square sq() function is used. Similarly, the data types used for the operator square are float, int, and double.

Square function
Square function
float c;
int a = 6;
void setup ( )
{
Serial.begin( 9600 );
c = sq(a);
Serial.println(c);
}
void loop ( )
{
// your code here
}
// output is 36.00
Implementation of square function

Square root operator

The square root operator is used to calculate the square root of a number. For calculating square the sqrt() function is used. Similarly, the data types used for the operator square are float, int, and double.

Square root function
Square root function
float c;
int a = 6;
void setup ( )
{
Serial.begin( 9600 );
c = sqrt(a);
Serial.println(c);
}
void loop ( )
{
// your code here
}
// output is 2.45
Implementation of square root function

Modulo operator

The modulo operator is the remainder when two values are divided, yet they are not divided completely. As a result, a remainder value is left to find that value remainder operator is used by using a percentage symbol %. The remainder value will be returned if both numbers are not entirely divisible.

Modulo operator
Modulo operator
float c;
int a = 7;
int b = 3;
void setup ( )
{
Serial.begin( 9600 );
c = a % b;
Serial.println(c);
}
void loop ( )
{
// your code here
}
// output is 1.00
Implementation of modulo operator

Power operator

The power operator is used to raise a number to the power of another. It calculates the value of variables and constants when they are in exponential form. For solving exponential forms, the pow() function is used.

Power function
Power function
float c;
int a = 7;
int b = 3;
void setup ( )
{
Serial.begin( 9600 );
c = pow(a,b);
Serial.println(c);
}
void loop ( )
{
// your code here
}
// output is 343.00
Implementation of power function

Absolute operator

The absolute operator returns the absolute value of the given variable or constant. This operator is used to convert a negative value to a positive. For this operation abs() is used.

Absolute operator
Absolute operator
float c;
int a = -6;
void setup ( )
{
Serial.begin( 9600 );
c = abs(a);
Serial.println(c);
}
void loop ( )
{
// your code here
}
// output is 6.00
Implementation of absolute operator

Minimum and maximum operator

Using the max() and min() functions, maximum and minimum values between two given variables or constants can be calculated.

Maximum and Minimum operator
Maximum and Minimum operator
float c;
int a = 7;
int b = 3 ;
void setup ( )
{
Serial.begin( 9600 );
c = max(a,b);
Serial.println(c);
c = min(a,b);
Serial.println(c);
}
void loop ( )
{
// your code here
}
//output is 7.00 for max() and 3.00 for min()
Implementation of maximum and minimum operator

Understanding of the concept

Question 1

What will be the value of c? If c = pow(a,b), given that a = 9, b = 2.

Show Answer
Question 2

What will be the value of c? If c = sqrt(a), given that a = 9.

Show Answer

Conclusion

Arithmetic operators are vital in Arduino programming, facilitating mathematical calculations and data manipulation. Handle data types and potential overflows carefully due to limited resources. Mastering these operators empowers developers to create efficient and powerful Arduino projects.

For a guide on boolean operators in Arduino, you can check this out.

Copyright ©2024 Educative, Inc. All rights reserved