C++ Arithmetic Operators
In this page:
result = operand1 + operand2; // also - * / %
Addition and Subtraction
Addition (+) and subtraction (-) work exactly as expected on both integers and floating-point numbers, combining or offsetting two values.
These are the building blocks nearly every numeric calculation in a program starts from, whether it's totaling a cart or computing a difference in coordinates.
Example: Addition and Subtraction
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare sum and set it to 5 + 3
int sum = 5 + 3;
// Declare diff and set it to 5 - 3
int diff = 5 - 3;
// Print to the console with cout
std::cout << sum << " " << diff << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiplication and Division
Multiplication (*) and division (/) behave differently depending on the operand types: dividing two ints performs integer division, silently discarding any remainder, so 7 / 2 evaluates to 3, not 3.5.
To get a fractional result, at least one operand needs to be a floating-point type.
Example: Multiplication and Division
#include <iostream>
int main() {
std::cout << 7 / 2 << std::endl; // integer division: 3
std::cout << 7.0 / 2 << std::endl; // 3.5
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modulo Operator
The modulo operator (%) returns only the remainder left over from integer division and requires both operands to be integers — for example, 10 % 3 evaluates to 1.
A very common practical use is checking even/odd with n % 2 == 0, or wrapping a value into a fixed range like cycling through array indices.
Example: Modulo Operator
#include <iostream>
int main() {
std::cout << 10 % 3 << std::endl; // remainder: 1
int n = 8;
std::cout << (n % 2 == 0) << std::endl; // even check
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Increment and Decrement Operators
++ and -- adjust a variable by exactly 1, and their position matters: prefix (++x) increments the variable before its new value is used in the surrounding expression, while postfix (x++) uses the original value first and increments afterward.
This distinction matters most inside larger expressions like arr[i++], where the timing of the increment changes which element gets accessed.
Example: Increment and Decrement Operators
#include <iostream>
int main() {
int x = 5;
std::cout << ++x << std::endl; // prefix: increments first, prints 6
std::cout << x++ << std::endl; // postfix: prints 6, then becomes 7
std::cout << x << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compound Assignment Operators
Compound assignment operators like +=, -=, *=, and /= fold an operation and an assignment into one step, so total += price; is shorthand for total = total + price;.
Beyond saving keystrokes, this shorthand also reads more clearly as 'update this variable' rather than 'reassign this variable to a new expression involving itself'.
Example: Compound Assignment Operators
#include <iostream>
int main() {
int total = 100;
int price = 20;
total += price; // shorthand for total = total + price
std::cout << total << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Dividing two ints like
7 / 2and expecting 3.5, when the result is 3 because both operands are integers. - Using
%on doubles such as5.5 % 2, which does not compile;%only works on integers. - Dividing by zero with integers, which is undefined behavior and often crashes.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: