Arithmetic Operators in C++
Arithmetic operators in C++ are symbols used to perform mathematical operations on operands. These operators allow you to perform addition, subtraction, multiplication, division, and modulus operations.
Addition (+)
Adds two operands together.
int result = 5 + 3; // result will be 8
Subtraction (-)
Subtracts the right operand from the left operand.
int result = 7 - 4; // result will be 3
Multiplication (*)
Multiplies two operands.
int result = 6 * 2; // result will be 12
Division (/)
Divides the left operand by the right operand. If both operands are integers, the result will be an integer, with any remainder discarded.
int result = 10 / 3; // result will be 3
If you want to get a floating-point result, you can use floating-point operands:
double result = 10.0 / 3.0; // result will be approximately 3.33333
Modulus (%)
Returns the remainder of the division of the left operand by the right operand.
int result = 10 % 3; // result will be 1
These operators can be used with variables, constants, or expressions. They follow the usual rules of precedence and associativity. Additionally, parentheses can be used to enforce a specific order of evaluation.