C++ Arithmetic Operators
In this page:
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 <iostream>
int main() {
int sum = 5 + 3;
int diff = 5 - 3;
std::cout << sum << " " << diff << std::endl;
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
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: