> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/06.-arithmetic-operators.md).

# 06. Arithmetic Operators

Arithmetic operators let you do math on numeric values.

| Operator | Meaning             | Example | Result                        |
| -------- | ------------------- | ------- | ----------------------------- |
| `+`      | Addition            | `5 + 2` | `7`                           |
| `-`      | Subtraction         | `5 - 2` | `3`                           |
| `*`      | Multiplication      | `5 * 2` | `10`                          |
| `/`      | Division            | `5 / 2` | `2` (int division truncates!) |
| `%`      | Modulus (remainder) | `5 % 2` | `1`                           |

```cpp
#include <iostream>

int main() {
    int a = 10;
    int b = 3;

    std::cout << "a + b = " << a + b << '\n';
    std::cout << "a - b = " << a - b << '\n';
    std::cout << "a * b = " << a * b << '\n';
    std::cout << "a / b = " << a / b << '\n'; // 3, not 3.33
    std::cout << "a % b = " << a % b << '\n'; // 1 (remainder)

    return 0;
}
```

***

### ⚠️ The #1 beginner trap: integer division

If **both** operands are `int`, the result is an `int` — any decimal part gets chopped off (not rounded).

```cpp
#include <iostream>

int main() {
    int a = 7;
    int b = 2;

    std::cout << a / b << '\n'; // prints 3, NOT 3.5

    return 0;
}
```

**Fix:** make at least one operand a `double`.

```cpp
#include <iostream>

int main() {
    int a = 7;
    int b = 2;

    double result = (double)a / b; // cast a to double first
    std::cout << result << '\n';   // prints 3.5

    return 0;
}
```

***

### Increment / Decrement (`++` and `--`)

Shortcuts for adding or subtracting 1.

```cpp
#include <iostream>

int main() {
    int count = 5;

    count++; // same as count = count + 1
    std::cout << count << '\n'; // 6

    count--; // same as count = count - 1
    std::cout << count << '\n'; // 5

    return 0;
}
```

**Pre vs post increment** (a classic gotcha):

```cpp
int x = 5;
std::cout << x++ << '\n'; // prints 5, THEN increments (x becomes 6)
std::cout << ++x << '\n'; // increments FIRST (x becomes 7), THEN prints 7
```

***

### Compound assignment operators

Shorthand for "do the math, then assign it back."

| Operator | Same as     |
| -------- | ----------- |
| `a += 5` | `a = a + 5` |
| `a -= 5` | `a = a - 5` |
| `a *= 5` | `a = a * 5` |
| `a /= 5` | `a = a / 5` |
| `a %= 5` | `a = a % 5` |

```cpp
#include <iostream>

int main() {
    int score = 10;

    score += 5; // score is now 15
    score *= 2; // score is now 30

    std::cout << score << '\n';

    return 0;
}
```

***

### More cool examples

#### 1. A simple calculator

```cpp
#include <iostream>

int main() {
    double num1 = 12;
    double num2 = 5;

    std::cout << "Sum: " << num1 + num2 << '\n';
    std::cout << "Difference: " << num1 - num2 << '\n';
    std::cout << "Product: " << num1 * num2 << '\n';
    std::cout << "Quotient: " << num1 / num2 << '\n';

    return 0;
}
```

#### 2. Checking even or odd using `%`

```cpp
#include <iostream>

int main() {
    int number = 17;

    if (number % 2 == 0) {
        std::cout << number << " is even\n";
    } else {
        std::cout << number << " is odd\n";
    }

    return 0;
}
```

#### 3. Splitting seconds into minutes and seconds

```cpp
#include <iostream>

int main() {
    int totalSeconds = 125;

    int minutes = totalSeconds / 60; // int division: whole minutes
    int seconds = totalSeconds % 60; // remainder: leftover seconds

    std::cout << minutes << "m " << seconds << "s\n"; // 2m 5s

    return 0;
}
```

#### 4. A running total with compound operators

```cpp
#include <iostream>

int main() {
    int total = 0;

    total += 100; // deposit
    total -= 30;  // withdrawal
    total += 50;  // deposit

    std::cout << "Final balance: " << total << '\n'; // 120

    return 0;
}
```

***

### Quick Recap

* Basic operators: `+ - * / %`
* **Integer division truncates** — cast to `double` if you need decimals.
* `++` / `--` increment or decrement by 1 (watch pre vs post placement).
* Compound operators (`+=`, `-=`, etc.) shorten `a = a op b` to `a op= b`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/06.-arithmetic-operators.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
