C++ Arithmetic Operators
Arithmetic operators in C++ are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division.
List of Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
| + | Addition | int sum = 5 + 3; // sum = 8 |
| – | Subtraction | int diff = 5 – 3; // diff = 2 |
| * | Multiplication | int prod = 5 * 3; // prod = 15 |
| / | Division | int div = 10 / 2; // div = 5 |
| % | Modulus (remainder after division) | int rem = 10 % 3; // rem = 1 |
| ++ | Increment (adds 1 to a variable) | int x = 5; x++; // x = 6 |
| — | Decrement (subtracts 1 from a variable) | int x = 5; x–; // x = 4 |
Example Program
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "a + b = " << (a + b) << endl;
cout << "a - b = " << (a - b) << endl;
cout << "a * b = " << (a * b) << endl;
cout << "a / b = " << (a / b) << endl;
cout << "a % b = " << (a % b) << endl;
a++;
cout << "a after increment: " << a << endl;
b--;
cout << "b after decrement: " << b << endl;
return 0;
}
Output
a + b = 13 a - b = 7 a * b = 30 a / b = 3 a % b = 1 a after increment: 11 b after decrement: 2
Next Topic
Next, learn about C++ Comparison Operators.