![]() |
VOOZH | about |
Arithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands (generally numeric values). An operand can be a variable or a value. For example, '+' is used for addition, '-' is used for subtraction, '*' is used for multiplication, etc. Example:
30 10
Explanation: In the above code, the + operator is used to calculate the sum of two values 10 and 20. It returned the sum which was then stored in the variable sum and printed.
In C++, there are 5 primary arithmetic operators. Increment (++) and decrement (--) are separate unary operators.
Operator | Name of the Operators | Operation | Implementation |
|---|---|---|---|
+ | Addition | Used in calculating the Addition of two operands | x+y |
- | Subtraction | Used in calculating Subtraction of two operands | x-y |
* | Multiplication | Used in calculating Multiplication of two operands | x*y |
/ | Division | Used in calculating Division of two operands | x/y |
% | Modulus | Used in calculating Remainder after calculation of two operands | x%y |
| -- | Decrement | Decreases the integer value of the variable by one | --x or x -- |
++ | Increment | Increases the integer value of the variable by one | ++x or x++ |
The addition operator (+) is used to add two operands means it is a binary operator. This operator works on both integers and floating-point numbers.
15
The subtraction operator (-) is used to subtract one operand from another. It is also a binary operator and works on both integers and floating-point numbers.
10
The multiplication operator (*) is used to multiply two operands. It is also a binary operator and works on both integers and floating-point numbers.
12
The division operator (/) is used to divide one operand by another. The result of integer division will be an integer, discarding the remainder. For floating-point division, the result is a floating-point value.
5 3.33333
The modulus operator (%) is used to find the remainder when one number is divided by another. This operator only works with integers.
1
The increment operator (++) is used to add 1 to the given operand. It is a unary operator that works on a single operand. It works on both integers and floating-point numbers.
11
The increment operator can also be written before the operand. e.g. ++a. It is called preincrement.
The decrement operator (--) is used to subtract 1 from the given operand. It is a unary operator that works on a single operand. It works on both integers and floating-point numbers.
9
The decrement operator can also be written before the operand. e.g. --a. It is called predecrement.