Arithmetic Operators in C++
Arithmetic operators are used to perform common mathematical operations in C++. They include addition, subtraction, multiplication, division, and modulus.
Key Topics
List of Arithmetic Operators
The following are the arithmetic operators available in C++:
Operator | Description | Example |
---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus | a % b |
++ |
Increment | ++a or a++ |
-- |
Decrement | --a or a-- |
Examples
Example: Using Arithmetic Operators
#include <iostream>
int main() {
int a = 15;
int b = 4;
std::cout << "Addition: " << a + b << std::endl;
std::cout << "Subtraction: " << a - b << std::endl;
std::cout << "Multiplication: " << a * b << std::endl;
std::cout << "Division: " << a / b << std::endl;
std::cout << "Modulus: " << a % b << std::endl;
int c = 5;
std::cout << "Increment: " << ++c << std::endl;
std::cout << "Decrement: " << --c << std::endl;
return 0;
}
Output:
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Modulus: 3
Increment: 6
Decrement: 5
Explanation: The program demonstrates the use of arithmetic operators with variables a
, b
, and c
. Increment and decrement operators modify the value of c
by 1.
Key Takeaways
- Arithmetic operators perform mathematical calculations.
- Increment (
++
) and decrement (--
) operators increase or decrease a variable by 1. - Understanding operator precedence is crucial for complex expressions.