Modulo Operator

Dart Arithmetic Modulo Operator takes two numbers as operands and returns the remainder of the integer division of left operand by right operand. Integer division meaning the quotient is an integer.

Symbol

% symbol is used for Modulo Operator.

ADVERTISEMENT

Syntax

The syntax for Modulo Operator is

operand_1 % operand_2

The operands must be numbers.

Examples

In the following example, we take two integers:n1 and n2, and find the remainder of the integer division n1 / n2 using Arithmetic Modulo Operator.

main.dart

void main() {
    var n1 = 14;
    var n2 = 3;
    var output = n1 % n2;
    print('n1 % n2 = $output');
}

Output

n1 % n2 = 2

In the following example, we divide a value of type double by a value of type integer.

main.dart

void main() {
    var n1 = 14.52;
    var n2 = 3.1;
    var output = n1 % n2;
    print('n1 % n2 = $output');
}

Output

n1 % n2 = 2.119999999999999

Conclusion

In this Dart Tutorial, we learned how to use Modulo Operator to find the remainder in the integer division operation of given two numbers.