Modulo (%) & Integer Division (/) in C++
📌 Quick Definition (Answer-First)
When both operands are integers in C++, / computes the integer quotient (discarding decimals), while modulo % returns the remainder. Together with base 10 (% 10 and / 10), they power digit-extraction and divisibility algorithms.
How It Works & Why It Matters
n % 10 isolates the last digit of n, and n /= 10 strips that last digit away.
Modulo also tests evenness (n % 2 == 0) and divisibility (a % b == 0).
💻 Essential Digit & Divisibility Expressions
459 % 10 // 9 (last digit) 459 / 10 // 45 (remaining digits) n % 2 == 0 // true if n is even a % b == 0 // true if a is divisible by b
🏫 Classroom Example
Looping while (n > 0) with sum += n % 10; n /= 10; sums all digits of any positive integer.
⚖️ 7 / 2 vs. 7.0 / 2
7 / 2 performs integer division and equals 3; making at least one operand decimal (7.0 / 2) performs floating-point division and equals 3.5.
⚠️ Common Mistake in Exams & Homework
Trying to use % with double/float variables (invalid in C++) or dividing/modulo by 0.
🚀 Practice in ArkyEdu Labs & Related Guides
🎮 Launch Interactive Lab →
Structura Repetitivă while & Algoritmi pe Cifrele unui Număr
📖 Full Lesson & Curriculum →
Clasa a VII-a · Bucle while & Cifre Număr
🏫 Grade 7 Hub →
All modules & guides for this grade
🏫 Grade 8 Hub →
All modules & guides for this grade
📚 Top 10 C++ Errors Fixed →
Troubleshoot the 10 most common C++ mistakes in Grades 7–8 (Code::Blocks): = vs ==, stray semicolons after if/while, infinite loops, 5/2 integer division, and n = 0.
❓ Quick Questions & Answers
Does n % 10 modify the variable n?
No, % only calculates the remainder; to shorten n you must assign n = n / 10;
Related Glossary Terms
← Back to Full Glossary (A–Z)while vs. do-while Loops in C++ →
Both while and do-while repeat code as long as a condition holds true. A while loop checks the condition first (may run 0 times), whereas a do-while loop checks at the end (always runs at least once).
Variable & C++ Data Types (int, double, char, bool) →
A variable is a named container in RAM that holds a value which can change while a program runs. In C++, every variable must be declared with a specific data type before use.
Algorithm & Core Algorithm Properties →
An algorithm is a finite, unambiguous, step-by-step set of instructions that transforms input data into the desired output within a finite number of steps.
Assignment (=) vs. Equality Check (==) in C++ →
A single equals sign (=) is the assignment operator that stores a value into a variable (x = 5). Double equals (==) is the comparison operator that tests whether two values are equal (x == 5).