TeachMate
My ProgressJoin Session

Operators & Expressions

0/13 completed · ~0h 57m

All Topics
C++LearnOperators & ExpressionsSyllabus
1 / 13

Arithmetic Operators

beginner
5 min read

What you'll learn

  • Evaluate arithmetic expressions with correct precedence.
  • Predict when integer division truncates.
  • Update a value with a compound assignment operator.

C++ provides five basic arithmetic operators for performing mathematical calculations on numeric types.

Start With a Problem

You want to split a bill of 100 among 3 people. A calculator shows 33.33, but the same expression in C++ with whole numbers gives 33, because both operands are ints and the fraction is discarded.

int bill = 100, people = 3;
cout << bill / people;   // 33, not 33.33

double exact = 100.0 / 3;
cout << exact;           // 33.3333

Arithmetic in C++ depends on the types involved. The rest of this note covers each operator and how integer and floating-point math behave.

Watch for these in class: = vs == in conditions, a stray ; after if/for, uninitialized variables, and confusing & (bitwise) with && (logical). Demo each one live before students hit it.

Operators

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33
%Modulus10 % 31
int a = 10, b = 3;

a + b;   // 13
a - b;   // 7
a * b;   // 30
a / b;   // 3 (integer division — truncates toward zero)
a % b;   // 1 (remainder of 10 / 3)

Integer Division

When both operands are integers, the decimal part is discarded (truncated toward zero).

10 / 3  = 3       // not 3.333
7 / 2   = 3       // not 3.5
-7 / 2  = -3      // truncates toward zero

Modulus with Negative Numbers

The sign of the modulus result follows the dividend (left operand).

7 % 3  =  1
-7 % 3 = -1       // sign follows dividend
7 % -3 =  1

Floating-Point Division

If at least one operand is a floating-point type, the result is a floating-point division.

double a = 10.0, b = 3.0;
a / b;   // 3.33333

Operator Precedence

The multiplicative operators (*, /, %) have higher precedence than the additive operators (+, -).

2 + 3 * 4    // 14  (multiplication first)
(2 + 3) * 4  // 20  (parentheses override)

Common mistakes: integer division truncates (7 / 2 is 3) and dividing an integer by zero is undefined behavior.

Complete Program: Arithmetic Operators

#include <iostream>

int main() {
    int a = 17, b = 5;

    std::cout << "Sum        = " << a + b << '\n';
    std::cout << "Difference = " << a - b << '\n';
    std::cout << "Product    = " << a * b << '\n';
    std::cout << "Quotient   = " << a / b << '\n';
    std::cout << "Remainder  = " << a % b << '\n';
    return 0;
}

Key takeaway: Always use parentheses to clarify operator precedence — readable code is better than clever code, and integer division silently truncates toward zero.

Try it: Write an expression that calculates the remainder when 17 is divided by 5, and print the result.

TeachMate - Interactive learning platform for bachelor students

HomeLearnJoin Session|2 Subjects