Arithmetic Operators- Complete Guide to Mathematical Symbols
What Are Arithmetic Operators?
Arithmetic operators are the symbols that let you do math in programming. Addition, subtraction, multiplication, division—the basics you've known since grade school. But programming languages often include a few extras that make calculations faster and cleaner.
These operators work with numbers (integers, decimals, floats) and return a result. They're the foundation of any calculation you'll ever write in code.
The Six Core Arithmetic Operators
Every programming language supports these fundamental operators. Some syntax varies slightly between languages, but the logic stays the same.
Addition (+)
Adds two values together. Simple enough.
5 + 3 // equals 8
10.5 + 2.5 // equals 13.0
You can also use it to concatenate strings in some languages like JavaScript and Python.
"Hello" + " " + "World" // "Hello World"
Subtraction (-)
Subtracts the right value from the left. Nothing surprising here.
10 - 4 // equals 6
7 - 12 // equals -5
Multiplication (*)
The asterisk (*) is universal for multiplication. Some older languages used X or other symbols—thankfully, that's dead.
6 * 7 // equals 42
-3 * 5 // equals -15
Division (/)
Divides the left value by the right. Most languages return a floating-point number even when dividing integers that divide evenly.
20 / 4 // equals 5.0
7 / 2 // equals 3.5
Modulus (%)
Returns the remainder after division. This one's underused by beginners, but it's incredibly useful for checking divisibility, cycling through values, and creating patterns.
10 % 3 // equals 1 (10 divided by 3 is 3 remainder 1)
15 % 5 // equals 0 (15 divided by 5 is 3 remainder 0)
7 % 2 // equals 1 (odd number check)
Exponentiation (**)
Raises a number to a power. Python and some other languages use double asterisks. Others like JavaScript now support ** too.
2 ** 3 // equals 8 (2 to the power of 3)
5 ** 2 // equals 25 (5 squared)
10 ** -1 // equals 0.1 (10 to the power of -1)
Floor Division (//)
Some languages include floor division, which divides and rounds down to the nearest integer. Python has it. Java and JavaScript don't—use Math.floor() instead.
7 // 2 // equals 3 (not 3.5)
-7 // 2 // equals -4 (rounds down, not toward zero)
The negative number behavior trips people up. Floor division always rounds toward negative infinity.
Operator Precedence
Math has rules. PEMDAS applies: Parentheses first, then Exponents, then Multiplication/Division, then Addition/Subtraction. Left to right for operators at the same level.
2 + 3 * 4 // equals 14 (not 20)
(2 + 3) * 4 // equals 20
2 ** 3 * 2 // equals 8 (not 16)
2 * 3 + 4 / 2 // equals 8
When in doubt, add parentheses. Code that's obvious beats code that's clever.
Comparison Table: Arithmetic Operators Across Languages
| Operator | Python | JavaScript | Java | C++ |
|---|---|---|---|---|
| Add | + | + | + | + |
| Subtract | - | - | - | - |
| Multiply | * | * | * | * |
| Divide | / | / | / | / |
| Modulus | % | % | % | % |
| Exponent | ** | ** | Math.pow() | pow() |
| Floor Div | // | Math.floor() | (int)(a / b) | static_cast<int>(a / b) |
Common Mistakes to Avoid
- Integer division in Java/C: Dividing two integers truncates the decimal. Use doubles or cast if you need precision.
- Modulus with negatives: The sign of the result matches the dividend.
-7 % 3gives-1in Python, but2in JavaScript. - Division by zero: Will crash your program. Always check your denominators first.
- Confusing
=with==: One assigns, one compares. This mistake costs hours of debugging.
Getting Started: Practical Examples
Here's how to use these operators in a real function. This calculates the total price with tax:
function calculateTotal(price, taxRate, quantity) {
const subtotal = price * quantity;
const tax = subtotal * (taxRate / 100);
const total = subtotal + tax;
return total;
}
calculateTotal(29.99, 8.5, 3); // returns 97.62
Here's a quick way to check if a number is even or odd using modulus:
function isEven(num) {
return num % 2 === 0;
}
isEven(4); // true
isEven(7); // false
Need to swap two numbers without a temp variable? Arithmetic operators got you:
a = 10
b = 5
a = a + b // a = 15
b = a - b // b = 10
a = a - b // a = 5
Compound Assignment Operators
You'll use these constantly. They combine an operation with assignment:
x += 5meansx = x + 5x -= 3meansx = x - 3x *= 2meansx = x * 2x /= 4meansx = x / 4x %= 3meansx = x % 3
Cleaner code. Less typing. No reason not to use them.
Wrapping Up
Arithmetic operators are straightforward. You learned the basics years ago—programming just uses slightly different symbols. Master the ones in the table above, understand operator precedence, and watch out for integer division and negative modulus edge cases. That's it.