In this article, we are going to learn about arithmetic operators in C++. As the name says, Arithmetic operators are the operators, which are used to perform arithmetic operations on the data, like addition, subtraction, multiplication, division, etc.
Arithmetic Operators in C++
In the below table, you can find the different arithmetic operator symbols, and then we will also perform a simple program to understand the arithmetic operators. For sake of example in the table, consider a = 10 and b = 5.
|+||This operator is used for the addition operation||c = a+b; c contains 15 as the result|
|–||This operator is used for the subtraction operation||c = a-b; c contains 5 as the result|
|*||This operator is used for a multiplication operation||c = a*b; c contains 50 as the result|
|/||This operator is used for division operation||c = 10/5; c contains 2 as the result|
|%||This operator is used to get the remainder when we divide two numbers||c = a % b; c contains 0 as the result since 5 divides 10 and gives a remainder of 0|
|++||This operator is used for incrementation operation, i.e. adding 1 to some number||++a; now the variable a contains 11|
|—||This operator is used for decrement operation, i.e. subtracting 1 from some number||–b; now, the variable b contains 4|
As you can see in the above table, we have listed the arithmetic operator symbols, which can be used to carry out some arithmetic operations, like addition, subtraction, multiplication, division, etc.
Let’s have a look at a simple program, in which, we will try to perform these different arithmetic operations.
int a = 10, b = 2;
std::cout << “Addition Operation:”<<a+b << std::endl;
std::cout << “Subtraction Operation:”<<a-b << std::endl;
std::cout << “Multiplication Operation:”<<a*b << std::endl;
std::cout << “Division Operation:”<<a/b << std::endl;
std::cout << “modulus Operation:”<<a%b << std::endl;
std::cout << “Increment Operation:”<<++a << std::endl; // pre-increment
std::cout << “Decrement Operation:”<<–b<< std::endl;//pre-decrement
If you try to execute the above program, this is the output that you get.
As you can see, we were able to do some arithmetic operations on the data, and as and when required, you can make use of these operator symbols, to perform these arithmetic operations.
Note that with the unary increment and decrement operators, we have things like pre-increment, post-increment, and pre-decrement and post decrement. In the above program, we are doing pre-operations. You can explore more about that.
FAQs related to Arithmetic operators in C++
Q: What are arithmetic operators in C++?
Ans: Using the arithmetic operators, we can perform some arithmetic operations, like addition, subtraction, multiplication, division, etc.
Q: What are the different arithmetic operator symbols in C++?
Ans: The arithmetic operator symbols in C++ include the following symbols – (+, -, *, /, %, ++, –)