Skip to content
FiftyCalc Free calculators for all 50 states

Modulo Calculator

Test cases replayed : 4 of 4 match · Methodology · No outside source listed

Enter a number a and a modulus n to get a mod n and the quotient.

Result

Sample figures

a mod n (floored, takes the sign of n)—

Truncated remainder (takes the sign of a, like % in C, Java, JavaScript) —

  • Floored quotient ⌊a ÷ n⌋—

Estimate for planning only, not tax, legal, financial or medical advice. This calculator uses the method explained on this page.

Modulo Calculator

Number a (dividend): enter a number. Prefilled example: 17.

Modulus n (divisor, not 0): enter a number. Prefilled example: 5.

How to use this calculator

Fill in the fields in this order; each step takes you to its field.

  1. Number a (dividend): enter a number.
  2. Modulus n (divisor, not 0): enter a number.
  3. Read the result: a mod n (floored, takes the sign of n); the panel also shows Truncated remainder (takes the sign of a, like % in C, Java, JavaScript) and Floored quotient ⌊a ÷ n⌋.

How this calculator works

The modulo operation gives the remainder r in a = n·q + r. In the floored definition (Knuth), q = ⌊a ÷ n⌋ and r = a − n⌊a ÷ n⌋, so r has the sign of n: −17 mod 5 = 3.

Many programming languages (C, Java, JavaScript) truncate the quotient toward zero instead, so their remainder has the sign of a: −17 % 5 = −2. Python's % uses the floored rule.

For positive numbers both definitions agree: 17 mod 5 = 2.

The formula

  1. Mod=a−n×floor⁡(an)+0
Symbols used in the formula
SymbolMeaning
aNumber a (dividend)
nModulus n (divisor, not 0)
Moda mod n (floored, takes the sign of n)
floor(x)the largest whole number not above x.

Worked example

Number a (dividend)17
Modulus n (divisor, not 0)5
Truncated remainder (takes the sign of a, like % in C, Java, JavaScript)2
Floored quotient ⌊a ÷ n⌋3
  1. Mod=17−5×floor⁡(175)+0=2

a mod n (floored, takes the sign of n) 2

Each step uses the unrounded value of the step before it; figures on this page are rounded for display.

Frequently asked questions

What is 17 mod 5?

2, because 17 = 5 × 3 + 2.

What is −17 mod 5?

3 with the mathematical definition (−17 = 5 × (−4) + 3); −2 with the truncated remainder of C or JavaScript.

What is modulo used for?

Clock arithmetic (14 mod 12 = 2 o'clock), checking even and odd numbers (n mod 2), check digits and hashing.

Related calculators