TL ; DR:
The modulo operator (%
) calculates the remainder of dividing two values:
cout << 10 % 2; // Output: 0
cout << 15 % 4; // Output: 3
cout << 20 % 3; // Output: 2
It can also be used with variables:
int a = 2;
int b = 4;
cout << b % a; // Output: 0
cout << 11 % b; // Output: 3
Full lesson:
Remember how we first learn about the division of two integer numbers in primary school?
The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.
Here are some examples:
10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2
The modulo operator:
The modulo operator (%
) calculates the remainder of dividing two values:
cout << 10 % 2 << endl; // Output: 0
cout << 15 % 4 << endl; // Output: 3
cout << 20 % 3 << endl; // Output: 2
It can also be used with variables:
int a = 2;
int b = 4;
cout << b % a << endl; // Output: 0
cout << 11 % b << endl; // Output: 3
Integral Division:
Remember that in C++, division between integers with /
returns only the integral part of the result:
cout << 10 / 2 << endl; // Output: 5
cout << 15 / 4 << endl; // Output: 3
cout << 20 / 3 << endl; // Output: 6
Quotient and remainder:
In programming, we combine both these concepts to get the quotient and remainder of some divison:
// Let's divide 26 by 3:
int quotient = 26 / 3;
int remainder = 26 % 3;
cout << quotient << endl; // Output: 8
cout << remainder << endl; // Output: 2
Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!
Hint
Look at the examples above if you get stuck.
The modulo operator (%
) is a fundamental concept in programming, particularly in C++. It is used to find the remainder of a division operation. Understanding how to use the modulo operator is crucial for solving a variety of problems, such as determining if a number is even or odd, performing cyclic operations, and more.
Before diving into complex applications, it's essential to grasp the basic concept of the modulo operator. The modulo operation finds the remainder after division of one number by another. For example, 15 % 4
equals 3 because 15 divided by 4 is 3 with a remainder of 3.
Here are some simple examples:
cout << 10 % 2; // Output: 0
cout << 15 % 4; // Output: 3
cout << 20 % 3; // Output: 2
The key concept behind the modulo operator is its ability to return the remainder of a division operation. This can be particularly useful in various scenarios, such as:
if (num % 2 == 0)
index = (index + 1) % array_size
Let's see how to apply these concepts with clear examples:
int a = 2;
int b = 4;
cout << b % a; // Output: 0
cout << 11 % b; // Output: 3
Let's explore some examples and real-world use cases:
int num = 5;
if (num % 2 == 0) {
cout << num << " is even";
} else {
cout << num << " is odd";
}
// Output: 5 is odd
int index = 0;
int array_size = 5;
index = (index + 1) % array_size;
cout << index; // Output: 1
When using the modulo operator, be mindful of the following common pitfalls:
Best practices include:
Advanced techniques involving the modulo operator include:
For example, using modulo in a hashing function:
int hash_function(int key, int table_size) {
return key % table_size;
}
Here is a well-commented code snippet demonstrating the correct use of the modulo operator:
// Function to check if a number is even or odd
bool isEven(int num) {
return num % 2 == 0;
}
// Function to perform cyclic increment
int cyclicIncrement(int index, int size) {
return (index + 1) % size;
}
int main() {
int num = 5;
if (isEven(num)) {
cout << num << " is even";
} else {
cout << num << " is odd";
}
// Output: 5 is odd
int index = 0;
int array_size = 5;
index = cyclicIncrement(index, array_size);
cout << index; // Output: 1
return 0;
}
When debugging code involving the modulo operator, consider the following tips:
For testing, write test cases to cover various scenarios:
#include <cassert>
void testModulo() {
assert(10 % 2 == 0);
assert(15 % 4 == 3);
assert(20 % 3 == 2);
assert(11 % 4 == 3);
}
int main() {
testModulo();
cout << "All tests passed!";
return 0;
}
When approaching problems involving the modulo operator, consider the following strategies:
Mastering the modulo operator is essential for solving a wide range of programming problems. By understanding its basics, common pitfalls, and advanced techniques, you can write more efficient and effective code. Practice regularly to reinforce your knowledge and explore further applications of the modulo operator.
For further reading and practice problems, consider the following resources: