Learn C++
#include "operators/op_arithmetic.hpp"

Arithmetic Operators in C++

//concept

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.

//how it works
1

The five operators

+ - * / % cover addition through remainder. % accepts integers only — 7.5 % 2 refuses to compile.

2

Integer division truncates

Both operands int means the fractional part is discarded toward zero: 7 / 2 == 3 and -7 / 2 == -3.

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.

4

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.

//example
#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;
}
Output
3
1
3.5
1total / n divides two ints, truncating 3.5 down to 3.
2total % n yields the remainder 1 — 7 is odd, which is exactly what % tests.
3total / 2.0 promotes to double division, keeping the fractional .5.
//use cases
Averaging with (a + b) / 2.0 instead of / 2 to avoid silent truncation
Testing even/odd, wrapping circular buffers, and extracting digits with %
Stepping counters and indexes with ++ in tight loops
Accumulating running totals concisely with += inside loops

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself — building beats watching.