C++ Arithmetic Operators

C++ provides a set of arithmetic operators that allow us to perform mathematical operations on variables and constants. These operators are used to perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

Table of Arithmetic Operators

The table below shows the different arithmetic operators available in C++

OperatorNameDescriptionExample
+AdditionAdds two operands3 + 4 = 7
-SubtractionSubtracts the second operand from the first7 - 4 = 3
*MultiplicationMultiplies two operands3 * 4 = 12
/DivisionDivides the first operand by the second12 / 4 = 3
%ModuloDivides the first operand by the second and returns the remainder12 % 5 = 2
++IncrementAdds 1 to the operanda++, ++a
--DecrementSubtracts 1 from the operanda--, --a

Examples

Here are some examples of how to use arithmetic operators in C++:

1#include <iostream> 2 3int main() 4{ 5 int a = 3; 6 int b = 4; 7 8 // Addition 9 int c = a + b; 10 std::cout << "a + b = " << c << std::endl; 11 12 // Subtraction 13 c = a - b; 14 std::cout << "a - b = " << c << std::endl; 15 16 // Multiplication 17 c = a * b; 18 std::cout << "a * b = " << c << std::endl; 19 20 // Division 21 c = a / b; 22 std::cout << "a / b = " << c << std::endl; 23 24 // Modulo 25 c = a % b; 26 std::cout << "a % b = " << c << std::endl; 27 28 // Increment 29 a++; 30 std::cout << "a++ = " << a << std::endl; 31 32 // Decrement 33 b--; 34 std::cout << "b-- = " << b << std::endl; 35 36 return 0; 37}

The output of the above program will be:

1a + b = 7 2a - b = -1 3a * b = 12 4a / b = 0 5a % b = 3 6a++ = 4 7b-- = 3

It is important to note that the order of operations (also known as operator precedence) determines the order in which operations are performed. For example, in the expression 3 + 4 * 5, the multiplication 4 * 5 is performed first and the result is then added to 3. The same rule applies to all other arithmetic operations.