Coding/Learn/cpp/operators math
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnArithmetic, Modulo & Increment Operators
6 / 20 0%
Lesson 06 of 20
5 mins read

Arithmetic, Modulo & Increment Operators

PreviousNext Lesson
In Plain English

C++ provides a full suite of mathematical operators: addition (+), subtraction (-), multiplication (*), division (/), modulo remainder (%), and fast increment/decrement (++ and --). Watch out for integer division!

The Mental Model

"Integer division (7 / 2) is like cutting a physical pizza between 2 people without fractional slices—you both get 3 whole slices, and the remaining 1 slice is discarded into the modulo trash bin (7 % 2 = 1)."

Deep Dive: How It Works

Basic Arithmetic: +, -, *, /, % (modulo).

The Integer Division Trap: In C++, dividing two integers drops the decimal completely: 7 / 2 results in 3, NOT 3.5! To get 3.5, at least one number must be a float or double (7.0 / 2).

Modulo Operator (%): Returns the integer remainder after division. 10 % 3 = 1. Perfect for checking if a number is even (num % 2 == 0).

Increment & Decrement: count++ increases by 1; count-- decreases by 1.

Compound Assignment: x += 5 is shorthand for x = x + 5 (also -=, *=, /=).

Syntax Blueprint

int sum = 10 + 5;     // 15
int div = 7 / 2;      // 3 (truncated!)
double exact = 7.0/2; // 3.5
int rem = 7 % 2;      // 1 (remainder)
x++;                  // Adds 1

Use 7.0 for decimal division and % for integer remainders.

PointCore Rules to Remember

Point star
Integer Division Quirk: Dividing two ints truncates (chops off) all decimal digits without rounding.
Point star
Modulo for Parity: num % 2 == 0 checks if a number is even; num % 2 != 0 checks if odd.
Point star
Operator Precedence: * and / calculate before + and - (PEMDAS order of operations).

Common Beginner Traps & How to Fix Them

Mistake:Writing double result = 5 / 2; and wondering why result is 2.0

Why it happens: 5 / 2 evaluates as integer math FIRST (yielding 2), and is then cast to 2.0.

How to fix: Write double result = 5.0 / 2; to trigger floating-point division.

Pro Tip: Use parentheses to make math calculations explicitly clear: int total = (price * count) + tax;.

Live Interactive Example

Hit Run Code to see it live
Arithmetic and Modulo Math
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: Division calculates how many groups fit, and modulo calculates the remainder.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Calculate rectangle area and perimeter

  • PointDeclare int width = 8; and int height = 5;
  • PointCalculate int area = width * height; and int perimeter = 2 * (width + height);
  • PointPrint "Area: 40 | Perimeter: 26".
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates