Need to perform modulo operations in your application? This guide covers everything you need to know about modulo calculations via API, including the difference between mod and remainder, applications, and implementation examples.
What is Modulo?
The modulo operation (mod) returns the remainder after division of one number by another. For example, 17 mod 5 = 2, because 17 divided by 5 equals 3 with a remainder of 2.
a mod n = r
where: a = n × q + r
Examples:
17 mod 5 = 2 (17 = 5 × 3 + 2)
23 mod 7 = 2 (23 = 7 × 3 + 2)
10 mod 3 = 1 (10 = 3 × 3 + 1)
Modulo vs Remainder
For positive numbers, modulo and remainder are the same. For negative numbers, they can differ:
| Operation | Remainder (% in most languages) | True Modulo |
|---|---|---|
| -17 mod 5 | -2 | 3 |
| 17 mod -5 | 2 | -3 |
Using the Modulo Calculator API
TinyFn provides a simple endpoint for modulo calculations:
GET https://api.tinyfn.io/v1/math/modulo
Headers: X-API-Key: your-api-key
{
"dividend": 17,
"divisor": 5,
"quotient": 3,
"remainder": 2,
"modulo": 2
}
Parameters
| Parameter | Type | Description |
|---|---|---|
a |
number | Dividend (the number to divide) |
n |
number | Divisor (the number to divide by) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/modulo?a=17&n=5',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.remainder); // 2
console.log(data.quotient); // 3
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/modulo',
params={'a': 17, 'n': 5},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['remainder']) # 2
cURL
curl "https://api.tinyfn.io/v1/math/modulo?a=17&n=5" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Cycling/Wrapping: Array index wrapping, carousel navigation
- Even/Odd Check: n mod 2 = 0 means even
- Hash Tables: Index = hash mod table_size
- Cryptography: Modular arithmetic in encryption
- Time Calculations: 25 mod 24 = 1 (next day at 1am)
Best Practices
- Handle division by zero: Modulo by zero is undefined
- Know your language: Different languages handle negative mod differently
- Use for wrapping: Great for circular buffers and cycles
- Consider performance: Modulo is a basic operation but adds up
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-math": {
"url": "https://api.tinyfn.io/mcp/math/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all math tools available via MCP in our Math MCP Tools for AI Agents guide.
Try the Modulo Calculator API
Get your free API key and start calculating modulo in seconds.
Get Free API Key