Arithmetic Operators in C++
Arithmetic operators in C++ are used to perform basic mathematical operations on values or variables. These operators help us add, subtract, multiply, divide or find the remainder of numbers. These are the most commonly used operators in programming, especially in calculations and logic building.
Types of Arithmetic Operators
C++ provides the following arithmetic operators :
| Operator | Name | Meaning | Example |
|---|---|---|---|
+ |
Addition | Adds two values | 10 + 5 = 15 |
- |
Subtraction | Subtracts second value from first | 10 - 3 = 7 |
* |
Multiplication | Multiplies two values | 6 * 4 = 24 |
/ |
Division | Divides numerator by denominator | 15 / 2 = 7 (integer division) |
% |
Modulus | Gives remainder after division | 20 % 6 = 2 |
1. Addition (+)
Used to add values.
int a = 10, b = 5;
cout << (a + b); // 15
2. Subtraction (-)
Used to subtract one value from another.
int a = 10, b = 3;
cout << (a - b); // 7
3. Multiplication (*)
Used to multiply values.
int a = 6, b = 4;
cout << (a * b); // 24
4. Division (/)
Divides one number by another. If both numbers are integers, the decimal part is removed.
C++ performs integer division, which removes the decimal part.
int a = 15, b = 2;
cout << (a / b); // 7 (not 7.5)
Important
- To get decimal result, at least one value must be float or double.
- C++ does not have a separate floor division operator like Python.
5. Modulus (%)
Returns the remainder after division. Mostly used in logic building like checking even/odd or repetition patterns.
int a = 10, b = 3;
cout << (a % b); // 1
Example: Using Arithmetic Operators
Let’s take a simple example to understand how arithmetic operators work in C++.
#include <iostream>
using namespace std;
int main() {
int a = 20;
int b = 7;
cout << "Addition: " << (a + b) << endl;
cout << "Subtraction: " << (a - b) << endl;
cout << "Multiplication: " << (a * b) << endl;
cout << "Division: " << (a / b) << endl;
cout << "Remainder: " << (a % b) << endl;
return 0;
}
Output:
Addition: 27
Subtraction: 13
Multiplication: 140
Division: 2
Remainder: 6
Explanation:
- a + b adds 20 and 7 → 27
- a - b subtracts 7 from 20 → 13
- a * b multiplies 20 by 7 → 140
- a / b performs integer division (decimal part ignored)
- a % b gives remainder when 20 is divided by 7 → 6
Summary:
- Arithmetic operators help in performing mathematical operations.
- C++ has 5 arithmetic operators: +, -, *, /, %.
- C++ does not support floor-division operator
//like Python. - Integer division removes decimal part.
- Modulus (%) gives remainder and is useful for logic building (like even/odd).