C++ · Operators and expressions
Modulo and Integer Division
Compare integer and floating-point division, conversion timing, truncation toward zero and remainders, with examples and self-checks.
Practice this topic in the app Read the lesson in the app
Integer quotients and remainders
Imagine dividing 17 candies equally among 5 people: each person gets 3 candies (the quotient), and 2 are left over (the remainder). In C++:
17 / 5 = 3 (integer division, taking the integer part of the quotient)17 % 5 = 2 (modulo, taking the remainder)
A few super useful tricks:
• n % 2 == 0 judges that n is even; n % 2 != 0 judges that n is odd (also applies to negative odd numbers).
• n % 10 extracts the ones digit of n.
• n / 10 removes the ones digit of n (for example, 123/10=12).
• In a loop, i % 3 == 0 can perform an operation once every 3 iterations.
Note: % can only be used with integers, not with double.
Why is 5 / 2 still 2 when stored in double?
Evaluate the expression first, then store its result. Dividing two integers produces an integer quotient with the fractional part truncated toward zero. The receiving variable type does not change division that has already happened.
| Statement | Evaluate first | Stored value |
|---|---|---|
| double p = 5 / 2; | Integer quotient 2 | 2.0 |
| double q = 5.0 / 2; | Floating-point quotient 2.5 | 2.5 |
| double r = static_cast<double>(5 / 2); | 2 first, then convert | 2.0 |
To retain the fractional part here, convert an operand before division: static_cast<double>(a) / b. Truncation toward zero is not rounding down: -5 / 2 is -2 and -5 % 2 is -1. These examples assume a nonzero divisor and a representable quotient. Do not evaluate division outside those conditions. Digit-extraction examples assume nonnegative integers; a negative dividend need not have a nonnegative remainder.
Run and check: when to convert
#include <iostream>
int main() {
int a = 5, b = 2;
double p = a / b;
double q = static_cast<double>(a) / b;
std::cout << p << " " << q << "\n";
std::cout << -5 / 2 << " " << -5 % 2 << "\n";
}Converting an operand first gives 2.5; converting the integer quotient afterward cannot recover the discarded fraction.
Try before revealing each answer
Is double x = 7 / 4; equal to 1.75? How would you change it?
No: it is 1.0. Use 7.0 / 4 or convert an operand before division to obtain 1.75.
How many full groups of 5 can you make from 17 sweets, and how many remain?
17 / 5 is 3 and 17 % 5 is 2; 3 × 5 + 2 = 17.
Does -7 / 2 produce -4?
No: the result is -3. Integer division truncates toward zero, rather than toward negative infinity.
Grouping, digits and remainder examples
Example 1: Determine odd/even and extract the ones digit
#include <iostream>
using namespace std;
int main() {
int n = 137;
cout << n % 2 << endl; // 1: odd
cout << n % 10 << endl; // 7: ones digit
cout << n / 10 << endl; // 13: remove the ones digit
return 0;
}137 % 2 = 1 shows that 137 is odd; 137 % 10 = 7 extracts the ones digit; 137 / 10 = 13 removes the ones digit.
Example 2: Using modulo for loop grouping
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 9; i++) {
if (i % 3 == 0)
cout << i << " is a multiple of 3" << endl;
}
return 0;
}Output: 3 is a multiple of 3 / 6 is a multiple of 3 / 9 is a multiple of 3. i%3==0 is a very concise way to test divisibility.
Tips
- % can only be used with integers (int, long long); using % with double will cause a compilation error.
- n % 10 gets the ones digit, n / 10 removes the ones digit, n / 10 % 10 gets the tens digit; this is a commonly used "digit extraction" technique in competitions.
- Note modulo with negative numbers: -7 % 3 in C++ gives -1 (the remainder has the same sign as the dividend), which differs slightly from the mathematical definition.
- Integer division truncates toward zero: -7 / 2 = -3 (not -4).
- a % b == 0 is equivalent to "a is divisible by b", and is the standard way to test a multiple relationship.
Prerequisites
Technical references
Scope note
This lesson is written by InfoCode and is not an official syllabus; the reference level is a site mapping and does not represent official levels, certification or eligibility. Practice records do not establish mastery or eligibility.
Reference level: GESP Level 1 (site mapping)