In C++, arithmetic operators are used to perform mathematical operations on variables and values. These operators include addition, subtraction, multiplication, division, and modulus.
Understanding how these operators work is essential for performing calculations and managing data effectively.
1. Basic Arithmetic Operators
The basic arithmetic operators in C++ are:
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
– | Subtraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
2. Using Addition (+)
The addition operator + adds two values or variables.
#include <iostream> using namespace std; int main() { int a = 10, b = 5; int sum = a + b; cout << "Sum: " << sum << endl; return 0; }
Explanation:
- a + b computes the sum of a and b, which is assigned to sum.
Output:
Sum: 15
3. Using Subtraction (-)
The subtraction operator – subtracts the second operand from the first.
#include <iostream> using namespace std; int main() { int a = 10, b = 5; int difference = a - b; cout << "Difference: " << difference << endl; return 0; }
Explanation:
- a – b subtracts b from a, and the result is stored in difference.
Output:
Difference: 5
4. Using Multiplication (*)
The multiplication operator * multiplies two values or variables.
#include <iostream> using namespace std; int main() { int a = 10, b = 5; int product = a * b; cout << "Product: " << product << endl; return 0; }
Explanation:
- a * b computes the product of a and b, stored in product.
Output:
Product: 50
5. Using Division (/)
The division operator / divides the first operand by the second. Note that dividing two integers will result in integer division, discarding any remainder.
#include <iostream> using namespace std; int main() { int a = 10, b = 3; int quotient = a / b; // Integer division cout << "Quotient: " << quotient << endl; return 0; }
Explanation:
- a / b performs integer division, so 10 / 3 results in 3 rather than 3.333.
Output:
Quotient: 3
For precise results with decimal points, use floating-point types:
#include <iostream> using namespace std; int main() { float a = 10.0f, b = 3.0f; float quotient = a / b; // Floating-point division cout << "Quotient: " << quotient << endl; return 0; }
Output:
Quotient: 3.33333
6. Using Modulus (%)
The modulus operator % returns the remainder of a division between two integers. It is typically used with integers only.
#include <iostream> using namespace std; int main() { int a = 10, b = 3; int remainder = a % b; cout << "Remainder: " << remainder << endl; return 0; }
Explanation:
- a % b calculates the remainder of dividing a by b, resulting in 1 for 10 % 3.
Output:
Remainder: 1
7. Compound Assignment with Arithmetic Operators
C++ provides compound assignment operators for each arithmetic operation, combining the operator with =. The syntax x += y is equivalent to x = x + y, and similar syntax applies to other operators.
Compound Operator | Equivalent to |
---|---|
+= | x = x + y |
-= | x = x – y |
*= | x = x * y |
/= | x = x / y |
%= | x = x % y |
Example with += and *=:
#include <iostream> using namespace std; int main() { int a = 10; a += 5; // a = a + 5 cout << "After += 5: " << a << endl; a *= 2; // a = a * 2 cout << "After *= 2: " << a << endl; return 0; }
Output:
After += 5: 15 After *= 2: 30
8. Increment (++) and Decrement (–) Operators
The increment (++) and decrement (–) operators increase or decrease a variable by 1. These can be used in prefix (++x, –x) or postfix (x++, x–) forms.
- Prefix: The value is modified before being used in the expression.
- Postfix: The value is modified after being used in the expression.
#include <iostream> using namespace std; int main() { int x = 5; cout << "Initial x: " << x << endl; cout << "Postfix increment x++: " << x++ << endl; cout << "After postfix increment: " << x << endl; cout << "Prefix increment ++x: " << ++x << endl; cout << "After prefix increment: " << x << endl; return 0; }
Explanation:
- x++ (postfix) returns 5 initially, then increments x to 6.
- ++x (prefix) increments x to 7 before returning the new value.
Output:
Initial x: 5 Postfix increment x++: 5 After postfix increment: 6 Prefix increment ++x: 7 After prefix increment: 7
9. Arithmetic Operations with Different Data Types
C++ allows arithmetic operations between different data types. However, in mixed-type expressions, C++ will promote types to the “wider” type, according to the rules of type promotion.
#include <iostream> using namespace std; int main() { int a = 5; double b = 2.5; double result = a + b; // a is promoted to double cout << "Result: " << result << endl; return 0; }
Explanation:
- a is promoted to double, and the addition yields 7.5 instead of 7.
Output:
Result: 7.5
10. Summary Table of Arithmetic Operators in C++
Operator | Operation | Example | Description |
---|---|---|---|
+ | Addition | a + b | Adds two operands |
– | Subtraction | a – b | Subtracts second operand from the first |
* | Multiplication | a * b | Multiplies two operands |
/ | Division | a / b | Divides first operand by the second |
% | Modulus (remainder) | a % b | Returns remainder of division |
+= | Addition assignment | a += b | Adds right operand to left and assigns |
-= | Subtraction assignment | a -= b | Subtracts right operand from left and assigns |
*= | Multiplication assignment | a *= b | Multiplies and assigns |
/= | Division assignment | a /= b | Divides and assigns |
%= | Modulus assignment | a %= b | Computes modulus and assigns |
++ | Increment | ++a or a++ | Increases value by 1 (prefix or postfix) |
— | Decrement | –a or a– | Decreases value by 1 (prefix or postfix) |
Complete Example: Using Arithmetic Operators
This example demonstrates using various arithmetic operators to perform calculations.
#include <iostream> using namespace std; int main() { int num1 = 20, num2 = 7; cout << "Addition: " << num1 + num2 << endl; cout << "Subtraction: " << num1 - num2 << endl; cout << "Multiplication: " << num1 * num2 << endl; cout << "Division: " << num1 / num2 << endl; cout << "Modulus: " << num1 % num2 << endl; num1 += 5; // Compound assignment cout << "After += 5: " << num1 << endl; num2--; // Decrement cout << "After num2--: " << num2 << endl; return 0; }
Output:
Addition: 27 Subtraction: 13 Multiplication: 140 Division: 2 Modulus: 6 After += 5: 25 After num2--: 6
In C++, arithmetic operators are essential for performing mathematical operations, making them a fundamental part of programming.