🚀
ArkyEdu GLOSAR TIC
RO Practice Lab
Algorithms, C++ & Scratch Grades 7–8 RO Term: Operatorul Modulo (%) și Împărțirea Întreagă (/) în C++

Modulo (%) & Integer Division (/) in C++

💬 Share on WhatsApp
📌 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.

❓ 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)