Arithmetic Operators in C++
Arithmetic operators in the C++ programming language are used to apply or perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus (remainder) on variables defined or declared in a C++ program. Arithmetic operators can be used to apply arithmetic operators to programmer-declared parameter variables containing integer and floating-point data types (including int, float, and double).
Popular Arithmetic Operators
| Operator Symbol | Operator Name | Operator Description | Operator Example |
| + | Addition | Use to Adds two different int, double, float, long double operands. | p + q |
| – | Subtraction | Use to Subtracts the second operand from the first operand. Larger value subtracts with smaller value | p – q |
| * | Multiplication | Used to Multiplies two different integer operands. | p * q |
| / | Division | Used to Divides the first operand by the second user define operand value. | p / q |
| % | Modulus (Remainder) | Used to Returns the remainder of the division of two user defined operands with remainder or modulus. | p % q |
Addition (+) Arithmetic Operators in C++.
Used to add two individual integer, float, double, or long double data type variables with numeric values in a C++ program.
Example of Addition (+) Arithmetic Operators.
#include <iostream>
using namespace std;
int main() {
int p = 3, q = 4;
int total = p + q; // The total is = 7
cout << “The total is – ” <<total << endl; // Result – total – 7
return 0;
}
Subtraction (-) Arithmetic Operators in C++.
In a C++ program, subtracting two individual integer, float, double, or long double data type variables with numeric values is used to subtract the second smaller numeric value from the first larger one.
Example of Subtraction (-) Arithmetic Operators.
#include <iostream>
using namespace std;
int main() {
int p = 7, q = 2;
int subtract = p – q; // The subtract = 5
cout << “The subtraction is – ” <<subtract<< endl; // Output: subtract – 5
return 0;
}
Multiplication (*) Arithmetic Operators in C++.
In a C++ program, it is used to multiply two individual integer, float, double, or long double data type variable numeric values.
Example of Multiplication (*) Arithmetic Operators.
#include <iostream>
using namespace std;
int main() {
int p = 7, q = 4;
int multiplication = p * q; // multiplication is – 28
cout << “multiplication is – ” << multiplication << endl; // Result – multiplication – 28
return 0;
}
Division (/) Arithmetic Operators in C++.
In a C++ program, dividing two individual integer, float, double, or long double data type variable numeric values is performed by dividing the first larger numeric value by the second smaller value. If both operands in the division are integer data types, the output result will be an integer value with the fractional part automatically truncated. For floating-point division numeric values, the output result can include decimal values.
Example of Division (/) Arithmetic Operators.
#include <iostream>
using namespace std;
int main() {
// Here we perform the integer division operation
int p = 30, q = 3;
int division = p / q; // division is – 10
cout << “The integer division value is – ” << division << endl;
// Here we perform floating-point division.
double r = 9.0, s = 4.0;
double output = r / s; // Floating division is = 2.25
cout << “The floating division value is – ” << output << endl;
// Comparison of integer with floating division example
cout << “11 / 5 = ” << 11 / 5 << endl; // Integer division value is – 2
cout << “11.0 / 5 = ” << 11.0 / 5 << endl; // Floating-point division value is – 2.2…
return 0;
}
Integer division method.
In a C++ program, if both user-defined operands are integer data type values, the output result is also an integer value. The fractional part is truncated or removed. Only the rounded value is displayed.
The result of 11 / 5 is 2 (not 2.2).
Floating-point division method.
In a C++ program, if both user-defined operands are floating-point data type values, and at least one operand is a floating-point data type such as float or double, the output result will be a floating-point number that displays the rounded value, and also includes the fractional part.
The result of 11.0 / 5 is 2.2…
Modulus (%) Arithmetic Operators in C++.
In a C++ program, if both user-defined operands are integer data type values, the modulus operator displays the remainder value left after division between two integers.
Example of Modulus (%) Arithmetic Operators.
#include <iostream>
using namespace std;
int main() {
int p = 7, q = 3;
int modulus = p % q; // modulus is – 1
cout << “the Remainder of modulus is – ” << modulus << endl;
//other modulus operator examples
cout << “11 % 3 = ” << 11 % 3 << endl;
cout << “36 % 5 = ” << 36 % 5 << endl;
return 0;
}
Modulus operator rules in C++.
Remember, in a C++ program, modulus is only used or applied to integer data types like int, long, etc. You cannot use it for floating-point data type numeric values in a C++ program.
Here, p % q displays the reminder value of the variable p when divided by q.
Modulus operator rules example.
Here, 11 % 3 displays the reminder output as 2 because 11 ÷ 3 = 3, resulting in a reminder output of 2.
Here, 36 % 5 displays the reminder output as 1 because 36 ÷ 5 = 7, resulting in a reminder output of 1.
Arithmetic Operators Precedence in C++.
In C++ programs, arithmetic precedence operators have a special order of precedence display value, which determines the order in which arithmetic operations are performed in any programming expression in the current program. The basic rule here is that arithmetic operations such as multiplication, division, and modulus have higher precedence than addition and subtraction.
Example of Arithmetic Operators Precedence.
#include <iostream>
using namespace std;
int main() {
int p = 7, q = 8, r = 2;
int output = p + q * r; // output = 7 + (8 * 2) = 23
cout << “the output of the Precedence operator is – ” << output << endl; // output, Result is – 23
return 0;
}
Here in the above example.
Here, q * r is evaluated first because the multiplication operator is given higher priority than addition.
Arithmetic Operators Precedence Brackets.
You can modify the order of operations using the bracket () parenthesis symbol, so that a specific sequence of operations is executed.
Arithmetic Precedence Brackets example.
#include <iostream>
using namespace std;
int main() {
int p = 8, q = 4, r = 3;
int output = (p + q) * r; // result = (8 + 4) * 3 = 36
cout << ” the output is – ” << output << endl; // Result is – 36
return 0;
}
Compound assignment operators in C++.
C++ programming also supports multiple compound assignment operators, which combine user-defined and declared arithmetic operations with assignment parameters.
| Compound Assignment | Equivalent with | Example with | Compound Assignment operator Description |
| += | p = p + q | p += q | Here it Adds q value to p variable and assigns the result to p value. |
| -= | p = p – q | p -= q | Here it Subtracts q value from p value and assigns the result to p variable. |
| *= | p = p * q | p *= q | Here it Multiplies p by q variable value and assigns the result to p variable. |
| /= | p = p / q | p /= q | Here it Divides p value by q value and assigns the result to p value. |
| %= | p = p % q | p %= q | Here it Assigns the remainder of p divided by q to p. |
Compound assignment operators example.
#include <iostream>
using namespace std;
int main() {
int p = 36, q = 3;
p += q; // p = p + q => 39
cout << “the p += q expression – ” << p << endl;
p -= q; // p = p – q => 36
cout << “the p -= q expression – ” << p << endl;
p *= q; // p = p * q => 108
cout << “the p *= q expression – ” << p << endl;
p /= q; // p = p / q => 36
cout << “the p /= q expression – ” << p << endl;
return 0;
}
Detail explanation of Arithmetic Operators
| Operator name | Operators Description | Operators Example |
| + | Addition Operators – here it Adds two different numeric operands | p + q |
| – | Subtraction Operators – here it Subtracts second operand from first operand | p – q |
| * | Multiplication Operators – here it Multiplies two different integer operands | p * q |
| / | Division Operators – here it Divides first operand by second operand | p / q |
| % | Modulus Operators – here it Returns remainder of division value | p % q |
