In this C++ tutorial, you will learn how the modulus operator % returns the remainder of integer division, which operand types it accepts, how it behaves with negative integers, and how to use it safely in common C++ programs.
C++ Modulus Operator and the Remainder of Integer Division
In C++, the % operator computes the remainder produced by integer division. For example, when 13 is divided by 5, the integer quotient is 2 and the remainder is 3. Therefore, 13 % 5 evaluates to 3.
The operator is often called the modulus or modulo operator in everyday programming. More precisely, C++ defines % as the remainder operator for integral operands. This distinction becomes important when negative values are involved.
Syntax of C++ Modulus Operator
Following is the syntax of Arithmetic Modulus Operator in C++.
result = operand_1 % operand_2
operand_1 is the dividend and operand_2 is the divisor. The expression evaluates to the remainder left after integer division.
For integral operands where the division result is representable, C++ follows this relationship:
(a / b) * b + (a % b) == a
For a = 13 and b = 5, integer division gives 13 / 5 == 2, and the remainder is 13 % 5 == 3.
Integer Datatypes Accepted by the C++ Modulus Operator
The built-in C++ % operator requires integral operands. Common signed and unsigned integer types used with the operator include the following.
- signed char
- short
- int
- long
- long long
- unsigned char
- unsigned short
- unsigned int
- unsigned long
- unsigned long long
When the two operands have different integral types, the usual arithmetic conversions are applied before the remainder is calculated. The type of the expression therefore depends on the converted operand types rather than always being int.
1. C++ Modulus with Integer Values
You can use % with two integers to find the remainder after integer division. The following existing snippet shows the intended type relationship.
int = int % int
In the following program, we initialize two integer variables and pass them as operands to the modulus operator.
C++ Integer Modulus Program
#include <iostream>
using namespace std;
int main() {
int a = 13;
int b = 5;
int modulo = a%b;
cout << modulo;
}
Output
3
The result is 3 because 13 = (2 * 5) + 3.
2. C++ Modulus with Long and Unsigned Char
In this example, the operands have the types long and unsigned char. Both are integer types, so the remainder expression is valid.
C++ Modulus Program with Mixed Integer Types
#include <iostream>
using namespace std;
int main() {
long a = 13;
unsigned char b = 5;
int modulo = a%b;
cout << modulo;
}
Output
3
Both operands are integral types, so the expression is valid. C++ converts the operands according to its usual arithmetic conversion rules before evaluating %.
3. C++ Modulus with Floating-Point Values Is Not Allowed
The built-in modulus operator does not accept floating-point operands. The following existing example uses a float as one operand, so the expression does not compile.
C++ Modulus Program That Produces a Floating-Point Operand Error
#include <iostream>
using namespace std;
int main() {
float a = 13;
unsigned char b = 5;
int modulo = a%b;
cout << modulo;
}
Output
You will get compilation error similar to the following output.
d:\workspace\cpp\main.cpp: In function 'int main()':
d:\workspace\cpp\main.cpp:8:18: error: invalid operands of types 'float' and 'unsigned char' to binary 'operator%'
8 | int modulo = a%b;
| ~^~
| | |
| | unsigned char
| float
The terminal process terminated with exit code: 1
The built-in % operator does not accept float or double operands. For floating-point remainders, use std::fmod from the <cmath> header.
C++ Floating-Point Remainder with std::fmod
#include <cmath>
#include <iostream>
using namespace std;
int main() {
double a = 13.5;
double b = 5.0;
double remainder = fmod(a, b);
cout << remainder;
}
Output
3.5
C++ Modulus with Negative Integers
With signed integers, C++ integer division truncates the quotient toward zero, and the remainder has the same sign as the dividend when the remainder is nonzero. Therefore, a C++ remainder can be negative.
#include <iostream>
using namespace std;
int main() {
cout << 13 % 5 << '\n';
cout << -13 % 5 << '\n';
cout << 13 % -5 << '\n';
cout << -13 % -5;
}
Output
3
-3
3
-3
If an algorithm requires a non-negative result for a positive modulus m, normalize the remainder explicitly.
normalized = ((value % m) + m) % m;
For example, with value = -13 and m = 5, the C++ remainder is -3, while the normalized result is 2.
C++ Modulus by Zero Is Invalid
The divisor used with % must not be zero. Evaluating an integral remainder expression such as a % 0 has undefined behavior. Check the divisor before applying the operator when zero is possible at runtime.
#include <iostream>
using namespace std;
int main() {
int a = 13;
int b = 0;
if (b != 0) {
cout << a % b;
} else {
cout << "Divisor must not be zero";
}
}
Chaining the C++ Modulus Operator
You can chain Modulus Operator and perform the modular division of more than two operands in a single statement. The pseudo code is given below.
result = operand_1 % operand_2 % operand_3 % ... % operand_n
The % operator is left-associative, so a % b % c is evaluated as (a % b) % c. It is not evaluated as a % (b % c).
C++ Chained Modulus Program
#include <iostream>
using namespace std;
int main() {
int a = 13;
int b = 5;
int c = 2;
int modulo = a % b % c; // a % b is 3, 3 % c is 1
cout << modulo;
}
Output
1
Using C++ Modulus to Check Even and Odd Integers
A common use of the modulus operator is checking divisibility. An integer is even when its remainder after division by 2 is zero. Otherwise, it is odd.
#include <iostream>
using namespace std;
int main() {
int number = 17;
if (number % 2 == 0) {
cout << "Even";
} else {
cout << "Odd";
}
}
Output
Odd
Using C++ Modulo 1000000007 in Large Integer Calculations
Programming problems sometimes ask for an answer modulo 1000000007, commonly written as 10^9 + 7. In such cases, apply the modulus as required by the algorithm instead of waiting until after an already-overflowed calculation.
#include <iostream>
using namespace std;
int main() {
const long long MOD = 1000000007LL;
long long a = 1000000000LL;
long long b = 1000000000LL;
long long sum = (a % MOD + b % MOD) % MOD;
long long product = ((a % MOD) * (b % MOD)) % MOD;
cout << sum << '\n';
cout << product;
}
Output
999999993
49
Using % MOD reduces values according to modular arithmetic, but it does not automatically make every intermediate expression overflow-safe. Choose a sufficiently wide integer type for the multiplication or other operation that occurs before the remainder is taken.
C++ Modulus Operator Summary
a % breturns the remainder of integral division.- The built-in
%operator requires integral operands. - Use
std::fmodfor floating-point remainder calculations. - With signed integers, a nonzero remainder has the sign of the dividend.
- Do not use zero as the divisor in an integral remainder expression.
- Chained modulus expressions are evaluated from left to right.
- Use the modulus operator for divisibility checks, cyclic indexing, and modular arithmetic when those operations fit the algorithm.
In this C++ Tutorial, we learned how to use C++ Modulus Operator, and the list of allowed datatypes for modulus operator in C++, with example C++ programs.
TutorialKart.com