Skip to content

Modulo Calculator

Type a number and what to divide it by to get a mod n, the quotient and the a = q × n + r check, with negative numbers worked out every common way. Switch to Advanced for power mod, inverses, congruences, a clock picture and a table.

Power mod, inverse, congruence, clock, table
What do you want to work out?

Whole numbers, decimals (use a dot) and negatives all work, up to 120 digits.

ResultUpdates as you type—Answer——————Working

    How your mod is worked out

    Your numbers, step by step: divide, round down, multiply back, take away.

    1. 1

      Divide

      17 ÷ 5how many 5s fit?= 3.4

    2. 2

      Round down

      ⌊3.4⌋drop the decimalsq = 3

    3. 3

      Multiply back

      3 × 5the part that fits= 15

    4. 4

      Take away

      17 − 15what is left over17 mod 5 = 2

    Read more: the formula, a worked example, negative numbers, programming languages, powers and inverses

    The modulo (or mod) of two numbers is the remainder after dividing one by the other. You write it a mod n, where n is called the modulus. It is the maths behind clocks (15:00 is 3 o’clock because 15 mod 12 = 3), days of the week, odd and even numbers, check digits and computer code.

    The formula

    a mod n = a − n × q, where q = a ÷ n rounded down to a whole number
    so a = q × n + r, with r between 0 and n (not reaching n)
    truncated rule (JavaScript, C, Java %): q = a ÷ n with the decimals cut off
    Euclidean rule: r is always 0 or more
    power mod: square and multiply through b in binary, taking mod n each time
    inverse: x with a × x mod n = 1, found by the extended Euclidean algorithm

    Worked example

    17 mod 5. Divide: 17 ÷ 5 = 3.4. Round down: q = 3. Multiply back: 3 × 5 = 15. Take away: 17 − 15 = 2. So 17 mod 5 = 2 and the check is 3 × 5 + 2 = 17. On a clock with 5 places (0 to 4), 17 steps from 0 go round 3 full times and land on 2.

    With a negative number, −17 mod 5: −17 ÷ 5 = −3.4, and rounding down gives −4 (down means toward minus infinity, not toward 0). Then −4 × 5 = −20 and −17 − (−20) = 3. Check: −4 × 5 + 3 = −17. JavaScript, C and Java cut −3.4 to −3 instead, so −17 % 5 gives −17 − (−15) = −2 there.

    Power mod, 1713 mod 5: first 17 mod 5 = 2, and 13 in binary is 1101. Start with 2. Next bit 1: square, 2² = 4, then multiply, 4 × 2 = 8, and 8 mod 5 = 3. Next bit 0: square only, 3² = 9 and 9 mod 5 = 4. Last bit 1: 4² = 16 and 16 mod 5 = 1, then 1 × 2 = 2. So 1713 mod 5 = 2 (1713 is 9,904,578,032,905,937, and it ends in 7, so it leaves 2 after dividing by 5).

    Inverse of 17 mod 5: 17 mod 5 = 2. Euclid: 5 = 2 × 2 + 1, then 2 = 2 × 1 + 0, so the gcd is 1 and an inverse exists. Working back: 1 = 5 − 2 × 2, which with the original number is 1 = 7 × 5 − 2 × 17. So 17 × (−2) leaves 1, and −2 mod 5 = 3. Check: 17 × 3 = 51 and 51 mod 5 = 1.

    The three rules for negative numbers

    SumFloored (maths, Python)Truncated (JavaScript, C, Java)Euclidean
    17 mod 5222
    −7 mod 32−12
    7 mod −3−211
    −7 mod −3−1−12

    All of them pass the check a = q × n + r; only the quotient q differs. Floored takes the sign of n, truncated takes the sign of a, and Euclidean is never negative. In Python, -7 % 3 is 2; in JavaScript, C and Java, -7 % 3 is −1. To get the maths answer in those languages, use ((a % n) + n) % n.

    Where mod is used

    • Even or odd: a number is even when n mod 2 = 0.
    • Clocks and calendars: 9 o’clock + 5 hours = 14 mod 12 = 2 o’clock; 10 days after a Monday is a Thursday because 10 mod 7 = 3.
    • Check digits: ISBN-10 uses mod 11, IBAN bank numbers use mod 97.
    • Codes and encryption: RSA uses power mod and inverses mod with numbers hundreds of digits long.

    All answers are exact: the calculator works with whole numbers of any size up to 120 digits and turns decimals into whole numbers first (7.5 mod 2 is worked out as 75 mod 20 = 15, which is 1.5), so nothing is rounded.

    Common questions

    What does mod mean in maths?

    a mod n is the remainder when you divide a by n. 17 mod 5 = 2, because 5 goes into 17 three times (15) and 2 is left over. You can check it: 3 × 5 + 2 = 17.

    How do you work out a mod n?

    Divide a by n, round the answer down to a whole number, multiply that back by n and take it away from a. For 17 mod 5: 17 ÷ 5 = 3.4, round down to 3, 3 × 5 = 15, and 17 − 15 = 2.

    What is −7 mod 3?

    In maths (and in Python) −7 mod 3 = 2, because −7 = −3 × 3 + 2 and the remainder takes the sign of the number you divide by. In JavaScript, C and Java, −7 % 3 gives −1, because those languages cut the quotient toward zero: −7 = −2 × 3 − 1. Both are correct; they follow different rules.

    Is mod the same as remainder?

    For positive numbers, yes. They only differ when a number is negative. The maths mod gives an answer with the same sign as n (or 0), while the % remainder in JavaScript, C and Java gives an answer with the same sign as a.

    Can you do mod with decimals?

    Yes. 7.5 mod 2 = 1.5, because 3 × 2 = 6 and 7.5 − 6 = 1.5. The quotient is still a whole number. This calculator works with exact decimals, so 0.3 mod 0.1 is exactly 0 (a computer’s floating-point maths often says 0.0999…).

    What is a modular inverse?

    The inverse of a mod n is the number x with a × x mod n = 1. The inverse of 3 mod 11 is 4, because 3 × 4 = 12 = 11 + 1. It only exists when a and n share no factor other than 1, and you find it with the extended Euclidean algorithm.

    Related calculators

    Long division calculatorThe full written long division step by step, with remainders, decimals, repeating decimals, short division and polynomials.LCM & HCF (GCF) calculatorLCM and HCF (GCF, GCD) of 2 to 100 numbers with prime factor trees, the ladder method, Euclid’s steps, fractions and word problems.Prime factorization calculatorPrime factors with powers, a drawn factor tree and the ladder, prime check, every factor and factor pair, simplified roots, primes near and between, and huge numbers.Exponent calculatorPowers with negative and fractional exponents, exact huge numbers, find the exponent or base, nth roots as surds, logs and antilogs in any base, with steps.

    Made by Zulu Calculator, part of AlphaZulu LLC. Formulas are shown in full and rule figures are checked against official sources; see how we build calculators.

    Embed this calculator on your site

    Free to use on your website or blog. Paste this code where you want the calculator to appear. Please keep the credit link.

    Modulo calculator (mod) by Zulu Calculator ↗