Arithmetic Operators in C++
There are five arithmetic operators in C++. They are:
Operator | Purpose |
---|---|
+ | addition |
– | subtraction |
* | multiplication |
/ | division |
% | remainder after integer division |
The % operator is also sometimes referred to as the modulus operator.
Example of the use of Arithmetic Operators:
Suppose that the x and y are integer variables whose values are 10 and 3 respectively. Several arithmetic expressions involving these variables are shown below, together with their resulting values:
Expression | Values |
---|---|
x + y | 13 |
x – y | 7 |
x * y | 30 |
x / y | 3 |
x % y | 1 |
In the above mentioned example, notice the truncated quotient resulting from the division operation (x / y), since both operands represent integer quantities. Also, notice the integer remainder resulting from the use of the modulus operator in the last expression (x % y).
Precedence of Arithmetic Operators:
Among the arithmetic operators, *, / and % fall into one precedence group, and + and – fall into another. The first group has a higher precedence than the second. Thus, multiplication, division and remainder operations will be carried out before addition and subtraction.
1 Response
[…] Arithmetic operators […]