Arithmetic operators are used to perform mathematical operations in C++. They include basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
In this tutorial, we will take a look at how to use these operators and how they can be used in C++ code.
Addition (+):
The addition operator is used to add two or more values together. For example:
int a = 10;
int b = 5;
int c = a + b; // c will equal 15
Subtraction (-)
The subtraction operator is used to subtract one value from another. For example:
int a = 10;
int b = 5;
int c = a - b; // c will equal 5
Multiplication (*)
The multiplication operator is used to multiply two or more values together. For example:
int a = 10;
int b = 5;
int c = a * b; // c will equal 50
Division (/)
The division operator is used to divide one numerical value by another. Here's an example:
int a = 10;
int b = 5;
int c = a / b; // c will equal 2
It's worth noting that if you divide two integers, the result will always be an integer. In the example above, 10 divided by 5 is 2, so the result of c will also be 2, not 2.5.
Modulus (%)
The modulus operator is used to get the remainder of a division operation. Here's an example:
int a = 10;
int b = 3;
int c = a % b; // c will equal 1
In this example, 10 divided by 3 is 3 with a remainder of 1, so the result of c will be 1.
That's a brief tutorial on arithmetic operators in C++. I hope this helps!