Given two positive numbers, (the dividend) and (the divisor), modulo (abbreviated as ) is the remainder of the Euclidean division of by . For example, the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a quotient of 3 and leaves a remainder of 0; there is nothing to subtract from 9 after multiplying 3 times 3. (Note that doing the division with a calculator will not show the result referred to here by this operation; the quotient will be expressed as a decimal fraction.)
Although typically performed with and both being integers, many computing systems allow other types of numeric operands. The range of numbers for an integer modulo of is 0 to . ( mod 1 is always 0; is undefined, possibly resulting in a division by zero error in programming languages.) See modular arithmetic for an older and related convention applied in number theory.
When either or is negative, the naive definition breaks down and programming languages differ in how these values are defined.
+ Integer modulo operators in various programming languages  
ABAP  MOD  Positive always 
ActionScript  %  Dividend 
Ada  mod  Divisor 
rem  Dividend  
ALGOL 68  ÷×, mod  Positive always 
AMPL  mod  Dividend 
APL  <nowiki></nowiki>  Divisor 
AppleScript  mod  Dividend 
AutoLISP  (rem d n)  Remainder 
AWK  %  Dividend 
BASIC  Mod  Undefined 
bash  %  Dividend 
bc  %  Dividend 
C (ISO 1990)  %  Implementationdefined 
div  Dividend  
C++ (ISO 1998)  %  Implementationdefined. "the binary % operator yields the remainder from the division of the first expression by the second. .... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementationdefined". 
div  Dividend  
C (ISO 1999)  %, div  Dividend openstd.org, section 6.5.5 
C++ (ISO 2011)  %, div  Dividend 
C#  %  Dividend 
Clarion  %  Dividend 
Clojure  mod  Divisor 
rem  Dividend  
COBOL  FUNCTION MOD  Divisor 
CoffeeScript  %  Dividend 
%%  Divisor CoffeeScript operators  
ColdFusion  %, MOD  Dividend 
Common Lisp  mod  Divisor 
rem  Dividend  
Construct 2  %  
D  %  Dividend 
Dart  %  Positive always 
remainder()  Dividend  
Eiffel  \\  Dividend 
Erlang  rem  Dividend 
Euphoria  mod  Divisor 
remainder  Dividend  
F#  %  Dividend 
FileMaker  Mod  Divisor 
Forth  mod  implementation defined 
Fortran  mod  Dividend 
modulo  Divisor  
Frink  mod  Divisor 
(GML)  mod, %  Dividend 
GDScript  %  Dividend 
Go  %  Dividend 
Haskell  mod  Divisor 
rem  Dividend  
Haxe  %  Dividend 
J  <nowiki></nowiki>  Divisor 
Java  %  Dividend 
Math.floorMod  Divisor  
JavaScript  %  Dividend 
Julia  mod  Divisor 
rem  Dividend  
LabVIEW  mod  Dividend 
LibreOffice  =MOD()  Divisor 
Lua 5  %  Divisor 
Lua 4  mod(x,y)  Divisor 
Liberty BASIC  MOD  Dividend 
Mathcad  mod(x,y)  Divisor 
Maple  e mod m  Positive always 
Mathematica  Mod[a, b]  Divisor 
MATLAB  mod  Divisor 
rem  Dividend  
Maxima  mod  Divisor 
remainder  Dividend  
Maya Embedded Language  %  Dividend 
Microsoft Excel  =MOD()  Divisor 
Minitab  MOD  Divisor 
Korn shell  %  Dividend 
Modula2  MOD  Divisor 
REM  Dividend  
MUMPS  #  Divisor 
Netwide Assembler (NASM, NASMX)  %  Modulo operator unsigned 
%%  Modulo operator signed  
Oberon  MOD  Divisor 
Object Pascal, Delphi  mod  Dividend 
OCaml  mod  Dividend 
Occam  \  Dividend 
Pascal (ISO7185 and 10206)  mod  Positive always 
Perl  %  Divisor 
PHP  %  Dividend 
PIC BASIC Pro  \\  Dividend 
PL/I  mod  Divisor (ANSI PL/I) 
PowerShell  %  Dividend 
Progress  modulo  Dividend 
Prolog (ISO 1995)  mod  Divisor 
rem  Dividend  
PureBasic  %,Mod(x,y)  Dividend 
Python  %  Divisor 
math.fmod  Dividend  
Racket  remainder  Dividend 
RealBasic  MOD  Dividend 
R  %%  Divisor 
Rexx  //  Dividend 
RPG  %REM  Dividend 
Ruby  %, modulo()  Divisor 
remainder()  Dividend  
Rust  %  Dividend 
Scala  %  Dividend 
Scheme  modulo  Divisor 
remainder  Dividend  
Scheme R^{6}RS  mod  Positive always r6rs.org 
mod0  Nearest to zero  
Seed7  mod  Divisor 
rem  Dividend  
SenseTalk  modulo  Divisor 
rem  Dividend  
Smalltalk  \\  Divisor 
rem:  Dividend  
Spin  //  Divisor 
SQL ()  mod(x,y)  Dividend 
SQL (SQL:2012)  %  Dividend 
Standard ML  mod  Divisor 
Int.rem  Dividend  
Stata  mod(x,y)  Positive always 
Swift  %  Dividend 
Tcl  %  Divisor 
Torque  %  Dividend 
Turing  mod  Divisor 
Verilog (2001)  %  Dividend 
VHDL  mod  Divisor 
rem  Dividend  
Vimscript  %  Dividend 
Visual Basic  Mod  Dividend 
x86 assembly  IDIV  Dividend 
XBase++  %  Dividend 
Mod()  Divisor  
Z3 theorem prover  div, mod  Positive always 
+ Floatingpoint modulo operators in various programming languages  
ABAP  MOD  Positive always 
C (ISO 1990)  fmod  Dividend "The fmod function returns the value x  i * y, for some integer i such that, if y is nonzero, the result as the same sign as x and magnitude less than the magnitude of y.". 
C (ISO 1999)  fmod  Dividend 
remainder  Nearest to zero  
C++ (ISO 1998)  std::fmod  Dividend 
C++ (ISO 2011)  std::fmod  Dividend 
std::remainder  Nearest to zero  
C#  %  Dividend 
Common Lisp  mod  Divisor 
rem  Dividend  
D  %  Dividend 
Dart  %  Positive always 
remainder()  Dividend  
F#  %  Dividend 
Fortran  mod  Dividend 
modulo  Divisor  
Go  math.Mod  Dividend 
Haskell (GHC)  Data.Fixed.mod'  Divisor 
Java  %  Dividend 
JavaScript  %  Dividend 
LabVIEW  mod  Dividend 
Microsoft Excel  =MOD()  Divisor 
OCaml  mod_float  Dividend 
Perl  POSIX::fmod  Dividend 
Perl6  %  Divisor 
PHP  fmod  Dividend 
Python  %  Divisor 
math.fmod  Dividend  
Rexx  //  Dividend 
Ruby  %, modulo()  Divisor 
remainder()  Dividend  
Scheme R^{6}RS  flmod  Positive always 
flmod0  Nearest to zero  
Standard ML  Real.rem  Dividend 
Swift  truncatingRemainder(dividingBy:)  Dividend 
XBase++  %  Dividend 
Mod()  Divisor 
In nearly all computing systems, the quotient and the remainder of divided by satisfy
However, this still leaves a sign ambiguity if the remainder is nonzero: two possible choices for the remainder occur, one negative and the other positive, and two possible choices for the quotient occur. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of or . Standard Pascal and ALGOL 68 give a positive remainder (or 0) even for negative divisors, and some programming languages, such as C90, leave it to the implementation when either of or is negative. See the table for details. modulo 0 is undefined in most systems, although some do define it as .
As described by Leijen,
For example, to test if an integer is odd, one might be inclined to test if the remainder by 2 is equal to 1:
return n % 2 == 1;}
But in a language where modulo has the sign of the dividend, that is incorrect, because when (the dividend) is negative and odd, mod 2 returns −1, and the function returns false.
One correct alternative is to test that it is not 0 (because remainder 0 is the same regardless of the signs):
return n % 2 != 0;}
Or, by understanding in the first place that for any odd number, the modulo remainder may be either 1 or −1:
return n % 2 == 1  n % 2 == 1;}
Examples (assuming is a positive integer):
In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations.
Optimizing may recognize expressions of the form expression % constant where constant is a power of two and automatically implement them as expression & (constant1). This can allow writing clearer code without compromising performance. This optimization is not possible for languages in which the result of the modulo operation has the sign of the dividend (including C), unless the dividend is of an Signedness integer type. This is because, if the dividend is negative, the modulo will be negative, whereas expression & (constant1) will always be positive.

