C++ Arithmetic Operators (original) (raw)
Last Updated : 21 Jan, 2025
Arithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands (generally numeric values). An operand can be a variable or a value. For example, ‘****+**’ is used for addition, '-' is used for subtraction, '*' is used for multiplication, etc. Let's take a look at an example:
C++ `
#include using namespace std;
int main() {
// Adding two integers using operator '+'
int sum = 10 + 20;
int diff = 20 - 10;
cout << sum << endl;
cout << diff;
return 0;
}
`
**Explanation: In the above code, the + operator is used to calculate the sum of two values **10 and **20. It returned the sum which was then stored in the variable **res and printed.
In C++, there are a total of 7 Arithmetic Operators as shown in the below table
Operator | Name of the Operators | Operation | Implementation |
---|---|---|---|
****+** | Addition | Used in calculating the Addition of two operands | x+y |
**- | Subtraction | Used in calculating Subtraction of two operands | x-y |
*** | Multiplication | Used in calculating Multiplication of two operands | x*y |
****/** | Division | Used in calculating Division of two operands | x/y |
% | Modulus | Used in calculating Remainder after calculation of two operands | x%y |
**-- | Decrement | Decreases the integer value of the variable by one | --x or x -- |
****++** | Increment | Increases the integer value of the variable by one | ++x or x++ |
Addition (+) Operator
The **addition operator (+) is used to add two operands means it is a binary operator. This operator works on both integers and floating-point numbers.Let's take a look at an example:
C++ `
#include using namespace std;
int main() { int a = 5, b = 10;
// Adding two integers a nd b
int res = a + b;
cout << res;
return 0;
}
`
2. Subtraction Operator (-)
The **subtraction operator (-) is used to subtract one operand from another. It is also a binary operator and works on both integers and floating-point numbers. Let's take a look at an example:
C++ `
#include using namespace std;
int main() { int a = 15, b = 5;
// Subtracting two integers a and b
int res = a - b;
cout << res;
return 0;
}
`
3. Multiplication Operator (*)
The **multiplication operator (*) is used to multiply two operands. It is also a binary operator and works on both integers and floating-point numbers. Let's take a look at an example:
C++ `
#include using namespace std;
int main() { int a = 4, b = 3;
// Multiplying two integers
int res = a * b;
cout << res ;
return 0;
}
`
4. Division Operator (/)
The **division operator (/) is used to divide one operand by another. The result of integer division will be an integer, discarding the remainder. For floating-point division, the result is a floating-point value. Let's take a look at an example:
C++ `
#include using namespace std;
int main() { int a = 10, b = 2;
// Dividing two integers
int res1 = a / b;
cout << res1 << endl;
double x = 10.0, y = 3.0;
// Floating-point division
double res2 = x / y;
cout << res2;
return 0;
}
`
5. Modulus Operator (%)
The **modulus operator (%)is used to find the remainder when one number is divided by another. This operator only works with integers. Let's take a look at an example:
C++ `
#include using namespace std;
int main() { int a = 10, b = 3;
// Finding the remainder
int res = a % b;
cout << res ;
return 0;
}
`
6. Increment Operator (++)
The **increment operator (++) is used to add 1 to the given operand. It is a binary operator that works only on single operand. It works on both integers and floating-point numbers. Let's take a look at an example:
C++ `
#include using namespace std;
int main() { int a = 10;
// Add 1 to a
a++; // It can also be written as ++a
cout << a ;
return 0;
}
`
The increment operator can also be written before the operand. e.g. ++a. It is called preincrement.
7. Decrement Operator (++)
The **decrement operator (--) is used to subtract 1 from the given operand. It is a binary operator that works only on single operand. It works on both integers and floating-point numbers. Let's take a look at an example:
C++ `
#include using namespace std;
int main() { int a = 10;
// Subtract 1 from a
a--; // it can also be --a;
cout << a ;
return 0;
}
`
The decrement operator can also be written before the operand. e.g. --a. It is called predecrement.