Supported forms: a + b mod m,
a - b mod m, a * b mod m,
a^e mod m, a^-1 mod m, and
a == b mod m. Every integer may have one
leading + or - sign.
Modular Arithmetic Calculator
Use this Modular Arithmetic Calculator to enter values, adjust options, and review results in a compact responsive workspace.
Results are calculated automatically as you enter data.
▼ See explanations and tips below ▼
Related Calculators
What Is Modular Arithmetic?
Modular arithmetic is arithmetic based on remainders. Instead of asking only for the full value of a calculation, modular arithmetic asks where that value lands after division by a fixed positive integer called the modulus.
A familiar example is clock arithmetic. On a 12-hour clock, 9 hours after 8 o’clock is not called 17 o’clock. It is 5 o’clock, because 17 and 5 land in the same position on a 12-hour cycle. In modular notation, that relationship is written as:
This reads as “17 is congruent to 5 modulo 12.” It means 17 and 5 have the same remainder when divided by 12.
Modular arithmetic is useful whenever numbers repeat in cycles or when only the remainder matters. It appears in calendars, clocks, divisibility tests, computer arithmetic, number theory, coding theory, and cryptography.
Why Modular Arithmetic Matters
Modular arithmetic gives a precise way to work with cycles. Days of the week repeat modulo 7, hours repeat modulo 12 or 24, and many computer systems store fixed-size integer values that wrap around after reaching a maximum.
It also lets large calculations become manageable. For example, instead of expanding a huge power such as \(3^{1000}\), modular arithmetic can reduce intermediate results along the way. This is one reason modular exponentiation is important in computational number theory and public-key cryptography.
For students, modular arithmetic is also a bridge between ordinary integer arithmetic and more abstract ideas such as equivalence classes, groups, modular inverses, and congruence equations.
Key Terms to Know
- Modulus: The positive integer that sets the cycle size. In \(a \pmod{m}\), the modulus is \(m\).
- Remainder: What is left after dividing one integer by another.
- Residue: A representative of a remainder class modulo \(m\). The least nonnegative residues are \(0, 1, 2, \ldots, m-1\).
- Congruence: A statement that two integers have the same remainder modulo \(m\), written \(a \equiv b \pmod{m}\).
- Least nonnegative residue: The standard residue in the range \(0\) through \(m-1\).
- Least positive representative: A representative in the range \(1\) through \(m\); a least nonnegative residue of \(0\) is shown as \(m\).
- Balanced representative: An equivalent residue chosen near zero, sometimes negative.
- Greatest common divisor: The largest positive integer that divides two integers, written \(\gcd(a,b)\).
- Modular inverse: A number that acts like a reciprocal modulo \(m\). The inverse of \(a\) modulo \(m\) is a number \(x\) such that \(ax \equiv 1 \pmod{m}\).
- Modular exponentiation: Computing \(a^e \bmod m\), often by reducing after repeated squaring instead of expanding the full power.
How Modular Arithmetic Works
The foundation of modular arithmetic is division with remainder. For an integer \(a\) and a modulus \(m > 1\), there is a quotient \(q\) and a remainder \(r\) such that:
The remainder \(r\) is the least nonnegative residue of \(a\) modulo \(m\).
For example:
So:
Negative numbers work too. The remainder must still be in the range \(0\) to \(m-1\) when using the least nonnegative residue. For example:
So:
A congruence can also be understood through divisibility:
That means \(a\) and \(b\) are congruent modulo \(m\) exactly when their difference is a multiple of \(m\).
Addition, Subtraction, and Multiplication
You can add, subtract, and multiply in modular arithmetic by doing the ordinary operation first, then reducing the result modulo \(m\).
If:
and:
then:
This is why you can reduce numbers before or after the calculation, as long as you are consistent about the same modulus.
Modular Inverses
A modular inverse of \(a\) modulo \(m\) is a number \(x\) that satisfies:
A modular inverse exists exactly when \(a\) and \(m\) are relatively prime:
When the inverse exists, it can be found using the extended Euclidean algorithm. The reason is Bézout’s identity: if \(\gcd(a,m)=1\), then there are integers \(x\) and \(y\) such that:
Reducing both sides modulo \(m\) gives:
So \(x\) is an inverse of \(a\) modulo \(m\).
Modular Powers
A modular power asks for:
When \(e\) is large, expanding the full value of \(a^e\) can be inefficient. Repeated squaring avoids this by reducing after each square and using the binary structure of the exponent.
For example, to compute \(3^{13} \bmod 7\), write:
Then reduce powers of 3:
Now combine the powers:
So:
Examples of Modular Arithmetic in Practice
Example 1: A Simple Remainder
Find the least nonnegative residue of \(47\) modulo \(9\).
Divide 47 by 9:
The remainder is 2, so:
Example 2: Clock Arithmetic
Suppose it is 10 o’clock on a 12-hour clock. What time is it 17 hours later?
Add the hours:
Reduce modulo 12:
So the clock lands on 3 o’clock:
Example 3: A Congruence Check
Check whether \(38\) and \(14\) are congruent modulo \(8\).
Compute the difference:
Since \(24\) is a multiple of \(8\), the two numbers are congruent:
You can also compare remainders:
Both reduce to 6, so the congruence is true.
Example 4: A Modular Inverse
Find the inverse of \(7\) modulo \(26\).
First check that an inverse can exist:
Now use the Euclidean algorithm:
Back-substitute to write 1 as a combination of 7 and 26:
So:
Since \(-11 \equiv 15 \pmod{26}\), the inverse is:
Check:
How to Interpret the Result
A result such as:
means that \(a\) and \(r\) land in the same remainder class modulo \(m\). If the result came from an operation, \(r\) is the reduced version of the operation’s raw value.
The least nonnegative residue is the standard representative in the range:
The least positive representative uses the range:
The main difference appears when the least nonnegative residue is 0. For example:
The least nonnegative residue is \(0\), but the least positive representative is \(5\).
A balanced representative chooses an equivalent residue closer to zero. For example, modulo 10, the least nonnegative residue of 8 is 8, but an equivalent balanced representative is \(-2\) because:
For congruence checks, a true result means both sides reduce to the same least nonnegative residue. A false result means the two sides land in different residue classes.
For modular inverses, a successful result means the value has a multiplicative inverse modulo \(m\). An error or no-inverse message usually means the value and modulus are not relatively prime.
Common Mistakes and Misconceptions
One common mistake is treating “mod” as ordinary division. Modular arithmetic keeps the remainder; it does not return the quotient.
Another mistake is assuming division always works. In modular arithmetic, division by \(a\) is really multiplication by the modular inverse of \(a\). If \(a\) has no inverse modulo \(m\), the division step is not valid.
Users also often confuse the residue 0 with the least positive representative. In least nonnegative mode, a multiple of the modulus has residue 0. In least positive mode, the same result may be shown as the modulus itself.
Negative values can also be confusing. A negative integer is not an error in modular arithmetic, but it must be normalized to the selected residue form. For example, \(-1 \equiv 4 \pmod{5}\) in least nonnegative form.
A practical input mistake is using decimals, fractions, variables, or multi-operation algebraic expressions when only integer patterns are supported. Modular arithmetic itself can be studied in broader settings, but this calculator is designed for integer arithmetic with specific expression forms.
Another easy mistake is entering one modulus in the expression and another in the separate modulus field. When an inline modulus such as mod n or % n is included at the end of the expression, that inline modulus is the one used.
When to Use Modular Arithmetic
Use modular arithmetic when:
- You need the remainder after division by a fixed integer.
- A situation repeats in cycles, such as clocks, calendars, or rotations.
- You want to test whether two integers are congruent modulo a number.
- You need to reduce sums, differences, products, or powers modulo \(m\).
- You are studying number theory, congruences, modular inverses, or modular exponentiation.
- You need to understand the arithmetic behind topics such as divisibility tests, residue classes, and cryptographic examples.
Limitations and Things to Keep in Mind
The modulus must be an integer greater than 1. Modulus 0, modulus 1, and negative moduli are not valid here.
The calculator works with plain signed decimal integers of up to 256 digits. Every operand and modulus may begin with one + or - sign; a leading + is equivalent to no sign. Decimals, fractions, scientific notation, variables, and arbitrary algebraic expressions are not supported. A congruence check may use =, ==, or ≡, and must compare numeric integer sides rather than expressions such as \(2x+1\).
The supported direct operations are single-value reduction, addition, subtraction, multiplication, powers, modular inverse forms, and numeric congruence checks. Division is not entered as a direct operation. To divide modulo \(m\), first determine whether the divisor has a modular inverse.
General negative exponents are not supported. The exponent \(-1\) is treated as a modular inverse request, but other negative exponents should not be used.
Arithmetic results are exact integer results, so ordinary decimal rounding does not apply. Display choices such as least nonnegative, least positive, or balanced representatives may change how the same residue class is shown, but they do not change the underlying congruence.
For balanced display, residues greater than half the modulus are shown by subtracting the modulus. With an even modulus, a residue exactly equal to half the modulus remains positive in this display style.
A clock visualization is helpful for small moduli, but it is only a visual aid. Large moduli can still have exact arithmetic results even when a clock diagram is not shown.
How to Use This Calculator
-
Enter a modular expression using plain signed decimal integers. Each integer may have one leading
+or-sign. Supported examples include a single integer,a + b,a - b,a * b, \(a^e\),inv(a), \(a^{-1}\), or a numeric congruence such as \(a \equiv b\). -
Enter the modulus in the modulus field, or append it to the expression using
mod nor% n. - Choose the residue display mode: least nonnegative, least positive, or balanced.
- Review the main result and status message.
- Use the residue table and step-by-step work to see how the raw values were reduced modulo \(m\).
- For small supported moduli, use the clock view to visualize where the residue lands on a modulo-\(m\) circle.
Frequently Asked Questions
What does \(a \equiv r \pmod{m}\) mean?
It means \(a\) and \(r\) have the same remainder when divided by \(m\). Equivalently, \(a-r\) is a multiple of \(m\).
What is the difference between a remainder and a residue?
A remainder is the value left by division, usually written in the range \(0\) through \(m-1\). A residue is a representative of a whole congruence class, so several different integers can represent the same residue class modulo \(m\).
Why does a modular inverse sometimes not exist?
A modular inverse of \(a\) modulo \(m\) exists only when \(\gcd(a,m)=1\). If \(a\) and \(m\) share a factor greater than 1, no integer \(x\) can make \(ax \equiv 1 \pmod{m}\).
Can I use division in modular arithmetic?
Division in modular arithmetic is handled by multiplying by a modular inverse. If the divisor has no inverse modulo \(m\), that division step is not valid.
Why can two different displayed answers both be correct?
Residues that differ by a multiple of the modulus are equivalent. For example, \(8\), \(-2\), and \(18\) are all congruent modulo \(10\). Different display modes choose different representatives of the same residue class.
Why is the clock view not always shown?
A clock diagram is useful only when the modulus is small enough to draw clearly. Even when the diagram is hidden, the arithmetic result can still be exact.
Sources and References
Books and Open Textbooks
- Oscar Levin. Discrete Mathematics: An Open Introduction. 4th ed., CRC Press / Open Math Books, 2025. Section 6.2, “Introduction to Number Theory.” https://discrete.openmathbooks.org/dmoi4/sec_addtops-numbth.html
- Al Doerr and Ken Levasseur. Applied Discrete Structures. LibreTexts / discretemath.org. Section 11.4, “Greatest Common Divisors and the Integers Modulo n.” https://math.libretexts.org/Bookshelves/CombinatoricsandDiscreteMathematics/AppliedDiscreteStructures%28DoerrandLevasseur%29/11%3AAlgebraicStructures/11.04%3AGreatestCommonDivisorsandtheIntegersModulon
- Harris Kwong. A Spiral Workbook for Discrete Mathematics. OpenSUNY / LibreTexts. Section 5.7, “Modular Arithmetic.” https://math.libretexts.org/Bookshelves/CombinatoricsandDiscreteMathematics/ASpiralWorkbookforDiscreteMathematics%28Kwong%29/05%3ABasicNumberTheory/5.07%3AModularArithmetic
- Jonathan A. Poritz. Yet Another Introductory Number Theory Textbook — Cryptology Emphasis. LibreTexts. Section 4.4, “Public-Key Crypto - the RSA Cryptosystem.” https://math.libretexts.org/Bookshelves/CombinatoricsandDiscreteMathematics/YetAnotherIntroductoryNumberTheoryTextbook-CryptologyEmphasis%28Poritz%29/04%3ACryptology/4.04%3APublic-KeyCrypto-theRSA_Cryptosystem