Skip to main content

Modulo Calculator

Free Modulo Calculator for arithmetic. Enter values to get step-by-step solutions with formulas and graphs. Free to use with no signup required.

Share this calculator

Formula

a mod b = a - b * floor(a / b)

The modulo operation returns the remainder after dividing a (dividend) by b (divisor). The mathematical modulo uses floored division to always produce a non-negative result when the divisor is positive, while the remainder operator in most programming languages uses truncated division which preserves the dividend sign.

Worked Examples

Example 1: Basic Modulo Calculation

Problem: Calculate 17 mod 5 and explain the result.

Solution: Division: 17 / 5 = 3 with remainder 2\nInteger quotient: floor(17 / 5) = 3\nRemainder: 17 - (5 x 3) = 17 - 15 = 2\nVerification: 5 x 3 + 2 = 17\nSo 17 mod 5 = 2\n17 is congruent to 2 (mod 5)

Result: 17 mod 5 = 2 | Quotient: 3 | 17 = 5 x 3 + 2

Example 2: Modulo with Negative Numbers

Problem: Calculate -7 mod 3 using both remainder and mathematical modulo.

Solution: Truncated division (remainder): -7 / 3 = -2.33, trunc = -2\nRemainder: -7 - (3 x -2) = -7 + 6 = -1\nSo -7 % 3 = -1 (JavaScript/C behavior)\n\nFloored division (math modulo): floor(-7 / 3) = -3\nModulo: -7 - (3 x -3) = -7 + 9 = 2\nSo -7 mod 3 = 2 (Python behavior)

Result: JS remainder: -1 | Math modulo: 2 | Both are valid in different contexts

Frequently Asked Questions

What is the modulo operation and how does it differ from division?

The modulo operation finds the remainder after dividing one number by another. While division asks how many times one number fits into another (producing a quotient), modulo asks what is left over after that division. For example, 17 divided by 5 gives a quotient of 3 with a remainder of 2, so 17 mod 5 equals 2. This operation is fundamental in mathematics and computer science, appearing in clock arithmetic, hash functions, cryptography, and cyclical patterns. The modulo operation essentially wraps numbers around a fixed range, making it perfect for any situation involving periodic or cyclical behavior.

What is the difference between remainder and modulo?

While often used interchangeably, remainder and modulo can produce different results with negative numbers. The remainder operation preserves the sign of the dividend: negative 7 remainder 3 equals negative 1 in most programming languages. The mathematical modulo always produces a non-negative result when the divisor is positive: negative 7 mod 3 equals 2 (since negative 7 equals negative 3 times 3 plus 2). JavaScript, C, and Java use truncated division for their percent operator (remainder), while Python and Ruby use floored division (true modulo). This distinction matters critically when working with negative numbers in programming and mathematical proofs.

How is modulo arithmetic used in everyday life?

Modulo arithmetic surrounds us in daily activities. Clock arithmetic is the most obvious example: 14 hours mod 12 equals 2, so 14:00 in 24-hour time is 2 PM. Days of the week cycle with mod 7, so if today is Wednesday (day 3) and you add 10 days, the day is (3 + 10) mod 7 = 6, which is Saturday. Calendar calculations, including leap year determination, rely heavily on modulo operations. ISBN and credit card check digits use modulo to detect errors. Even musical scales use mod 12, since there are 12 semitones in an octave, making music theory fundamentally modular.

How does the modulo operation work in programming?

In most programming languages, the modulo or remainder operator is represented by the percent symbol. In JavaScript, Python, C, Java, and many others, you write a % b to get the remainder of a divided by b. However, behavior with negative numbers varies by language. JavaScript and C use truncated division (remainder keeps dividend sign), while Python uses floored division (result has divisor sign). Common programming uses include checking if a number is even (n % 2 === 0), cycling through array indices (index % array.length), implementing circular buffers, formatting output in columns, and hash table implementations. Understanding these subtle differences prevents bugs in cross-language development.

How is the modulo operation used in cryptography?

Modular arithmetic forms the mathematical foundation of modern cryptography. RSA encryption relies on modular exponentiation: computing large powers modulo the product of two large primes. The Diffie-Hellman key exchange uses modular exponentiation over prime fields to establish shared secrets over insecure channels. Elliptic curve cryptography operates over finite fields defined by modular arithmetic. Hash functions use modular operations to compress arbitrary-length input into fixed-size output. The security of these systems depends on the computational difficulty of reversing modular operations, such as finding discrete logarithms or factoring large numbers, which are believed to be computationally infeasible for sufficiently large values.

What is the Euclidean algorithm and how does it use modulo?

The Euclidean algorithm is an efficient method for finding the greatest common divisor (GCD) of two numbers using repeated modulo operations. Starting with two numbers a and b, you compute a mod b, then replace a with b and b with the remainder, repeating until the remainder is zero. The last non-zero remainder is the GCD. For example, GCD(48, 18): 48 mod 18 = 12, then 18 mod 12 = 6, then 12 mod 6 = 0, so GCD is 6. This algorithm runs in logarithmic time relative to the smaller number and is over 2,300 years old, making it one of the oldest algorithms still in active use. The extended Euclidean algorithm also finds modular inverses.

References