`%`

)Learn how we can use the modulus operator to get the remainder of integer division, and some common use cases.

Updated

In this short lesson, we'll explore how the modulus operator helps in obtaining the remainder after division. This has many applications and is particularly useful when we’re solving problems using loops.

We can imagine that the answer to a division question like $5 / 3$ might be $1$, with $2$ left over. This is an example of * modular arithmetic*.

When performing division using modular arithmetic, we get two results:

- The quotient, which is $1$ in this case
- The remainder, which is $2$ in this case

We've previously seen that modular division is the default form used in C++ when working with integers.

The `/`

operator lets us get the quotient:

```
// This will be 1
int Quotient { 5 / 3 };
```

To get the remainder, we use the modulus operator, denoted by `%`

:

```
// This will be 2
int Remainder { 5 % 3 };
```

A common use of the modulus operator is to check if a number is even or odd. After being divided by $2$, an even number will have a remainder of $0$, whilst an odd number will have a remainder of $1$:

```
bool isEven(int Number) {
return Number % 2 == 0;
}
```

Test your Knowledge### Modulus Operator

After executing the following statement, what is the value of `Remainder`

?

`int Remainder { 5 % 6 };`

One of the useful properties of modular arithmetic is its tendency to "wrap around" after they reach a certain value.

Let's see an example:

```
0 % 3; // 0
1 % 3; // 1
2 % 3; // 2
3 % 3; // 0
4 % 3; // 1
5 % 3; // 2
6 % 3; // 0
7 % 3; // 1
8 % 3; // 2
```

We can see a repeating pattern developing here. As we increment the left operand, the modulus operator returns a repeating cycle of $0, 1, 2$. The length of this cycle is determined by the right operand. In this case, our right operand was $3$, so we got a repeating pattern of 3 numbers.

If our right operand were $2$ we’d get a repeating cycle of $0, 1, 2$. Were it $10$, our cycle would be $0, 1, …, 9$

This repeating pattern is one of the main reasons that the modulus is useful in programming - particularly when paired with loops.

The "wrapping around" nature of the modulus operator gives us an easy way to generate a boolean expression that is true on some subset of our loop iterations.

For example, if we increment a variable `i`

on every iteration of our loop, `i % 3`

will be `0`

on every third iteration.

Below, we use this calculation within an `if`

statement to insert a line break on every third iteration:

```
#include <iostream>
using namespace std;
int main(){
for (int i = 1; i <= 9; ++i) {
cout << i << " ";
if (i % 3 == 0) cout << '\n';
}
}
```

The above code inserts a new line on every 3rd iteration. The output looks like this:

```
1 2 3
4 5 6
7 8 9
```

The next lesson introduces a famous programming challenge where the modulus operator is crucial to solving the problem.

Was this lesson useful?

Updated

Lesson Contents### The Modulus Operator (

`%`

)Learn how we can use the modulus operator to get the remainder of integer division, and some common use cases.

This lesson is part of the course:### Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way