Arithmetic Operators
Arithmetic operators in C++ perform basic mathematical operations, including addition, subtraction, multiplication, division, modulus, increment, and decrement. These operators are commonly applied to variables and expressions in computations.
Arithmetic operators allow programmers to handle numerical computations and manipulate values effectively within code. Understanding how these operators function is crucial for building algorithms and solving problems in C++ programming.
The following table summarizes the seven arithmetic operators in C++:
Symbol | Name | Description | Implementation Example |
---|---|---|---|
+ |
Addition | Adds two values | a + b |
- |
Subtraction | Subtracts the second value from the first | a - b |
* |
Multiplication | Multiplies two values | a * b |
/ |
Division | Divides the first value by the second | a / b |
% |
Modulus | Returns the remainder of division | a % b |
++ |
Increment | Increases the value of a variable by 1 | ++a or a++ |
-- |
Decrement | Decreases the value of a variable by 1 | --a or a-- |
Explanation of Arithmetic Operators
Addition (+
)
The addition operator +
adds two operands together.
#include <iostream>using namespace std;int main() {int a = 10, b = 5;int sum = a + b;cout << "Sum: " << sum;return 0;}
Output:
Sum: 15
Subtraction (-
)
The subtraction operator -
subtracts the second operand from the first.
#include <iostream>using namespace std;int main() {int a = 10, b = 5;int diff = a - b;cout << "Difference: " << diff;return 0;}
Output:
Difference: 5
Multiplication (*
)
The multiplication operator *
multiplies two operands.
#include <iostream>using namespace std;int main() {int a = 10, b = 5;int product = a * b;cout << "Product: " << product;return 0;}
Output:
Product: 50
Division (/
)
The division operator /
divides the first operand by the second. If both operands are integers, the result will also be an integer, dropping any remainder. If either of the operand is a floating point number, then the result will be in decimals.
#include <iostream>using namespace std;int main() {int a = 10, b = 5;int quotient = a / b;cout << "Quotient: " << quotient;return 0;}
Output:
Quotient: 2
Note: When performing division, ensure the divisor is not zero, as this will result in a runtime error.
Modulus (%
)
The modulus operator %
returns the remainder of integer division.
#include <iostream>using namespace std;int main() {int a = 10, b = 3;int remainder = a % b;cout << "Remainder: " << remainder;return 0;}
Output:
Remainder: 1
Note: The modulus operator is only applicable to integer operands.
Increment (++
)
The increment operator ++
increases the value of a variable by 1. It can be used in two ways:
- Pre-increment (
++a
): Incrementsa
first, then uses its value. - Post-increment (
a++
): Uses the current value ofa
, then increments it.
#include <iostream>using namespace std;int main() {int a = 5;cout << "Pre-increment: " << ++a << endl; // Increments then printsa = 5;cout << "Post-increment: " << a++ << endl; // Prints then incrementscout << "After post-increment: " << a;return 0;}
Output:
Pre-increment: 6Post-increment: 5After post-increment: 6
Decrement (--
)
The decrement operator --
decreases the value of a variable by 1. It also has two types:
- Pre-decrement (
--a
): Decreasesa
first, then uses its value. - Post-decrement (
a--
): Uses the current value ofa
, then decreases it.
#include <iostream>using namespace std;int main() {int a = 5;cout << "Pre-decrement: " << --a << endl; // Decrements then printsa = 5;cout << "Post-decrement: " << a-- << endl; // Prints then decrementscout << "After post-decrement: " << a;return 0;}
Output:
Pre-decrement: 4Post-decrement: 5After post-decrement: 4
To continue learning about C++ operators and other fundamental programming concepts, check out the Learn C++ Course on Codecademy.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours