Arithmetic Operators in C++
C++ arithmetic uses the familiar five: + - * / % (modulo). The critical rule is that / on two integers performs truncating integer division — 7 / 2 is 3, not 3.5 — while mixing in any floating operand promotes the whole expression to floating point. The % operator works only on integers and yields the remainder, which is the backbone of divisibility tests, wrapping indexes, and digit extraction.
Compound forms (+=, -=, *=, /=, %=) combine operation with assignment, and prefix/postfix ++ and -- step a variable by one with subtly different timing: ++i increments before the value is used, i++ after.
The five operators
+ - * / % cover addition through remainder. % accepts integers only — 7.5 % 2 refuses to compile.
Integer division truncates
Both operands int means the fractional part is discarded toward zero: 7 / 2 == 3 and -7 / 2 == -3.
Mixed types promote
One double operand lifts everything to double: 7 / 2.0 == 3.5. The usual fix for accidental truncation is casting one side.
Compound and increment forms
x += 2 beats x = x + 2 for brevity; ++i vs i++ differ only in when the increment lands relative to the surrounding expression.
#include <iostream> int main() { int total = 7, n = 2; std::cout << total / n << std::endl; std::cout << total % n << std::endl; std::cout << total / 2.0 << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.