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

Free, Unlimited Access
3D art showing two RPG characters
Ryan McCombe
Ryan McCombe
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/35 / 3 might be 11, with 22 left over. This is an example of modular arithmetic.

Quotients and Remainders

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

  • The quotient, which is 11 in this case
  • The remainder, which is 22 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 22, an even number will have a remainder of 00, whilst an odd number will have a remainder of 11:

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

Divisibility

With modular arithmetic, the phrase "divisible by" is typically used to refer to a scenario where the modulo operator % would return 0. In our isEven() function, we deduced that Number is even if it is divisible by 2, that is, if Number % 2 returned 0.

More generally, xx is divisible by yy if x % y returns 0. For example, 66 is divisible by 11, 22, 33, and 66 because 6 % 1, 6 % 2, 6 % 3 and 6 % 6 all return 0 :

  • 6÷1=66 \div 1 = 6, with 00 remaining
  • 6÷2=36 \div 2 = 3, with 00 remaining
  • 6÷3=26 \div 3 = 2, with 00 remaining
  • 6÷6=16 \div 6 = 1, with 00 remaining

66 is not divisible by 44, 55, or 77, because neither 6 % 4, 6 % 5 nor 6 % 7 return 0:

  • 6÷4=16 \div 4 = 1, with 22 remaining, so 6 % 4 returns 2
  • 6÷5=16 \div 5 = 1, with 11 remaining, so 6 % 5 returns 1
  • 6÷7=06 \div 7 = 0, with 66 remaining, so 6 % 7 returns 6
Test your Knowledge

Modulus Operator

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

int Remainder { 5 % 6 };

The Cycle of the Modulus Operator

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,20, 1, 2. The length of this cycle is determined by the right operand. In this case, our right operand was 33, so we got a repeating pattern of 3 numbers.

If our right operand were 22 we’d get a repeating cycle of 00 and 11. Were it 1010, our cycle would be 0,1,,90, 1, …, 9

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

The Modulus Operator 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?

Next Lesson

Fizz Buzz

Put everything we've learned in this chapter to the test by creating a C++ version of the Fizz Buzz game.
Teacher working in a classroom
Ryan McCombe
Ryan McCombe
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.

3D art showing a progammer setting up a development environment
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

Free, Unlimited Access
Functions, Conditionals and Loops
3D art showing a progammer setting up a development environment
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

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Next Lesson

Fizz Buzz

Put everything we've learned in this chapter to the test by creating a C++ version of the Fizz Buzz game.
Teacher working in a classroom
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved