Math Operations — Your Code Calculator
Learn how to add, subtract, multiply, divide, and find remainders — the building blocks of every CP solution!
Open interactive version (quiz + challenge)Real-world analogy
Math operators in code are like a super-powered calculator that you program yourself. A regular calculator waits for you to press buttons, but YOUR calculator can do millions of calculations in one second — all by itself! Imagine telling a robot: 'Add up every number from 1 to a million.' A human would need weeks. Your code does it in a blink.
What is it?
Math operations are instructions that tell the computer to calculate things using numbers. The five main operators (+, -, *, /, %) let you do addition, subtraction, multiplication, division, and find remainders. They are the foundation of almost every CP problem you will ever solve.
Real-world relevance
Math operations are everywhere! Games use them to calculate damage and scores. GPS apps use them to find the shortest route. Banking apps use them for interest calculations. In CP, you might calculate the sum of an array, find the average, check if numbers are even or odd, or compute answers modulo a large prime.
Key points
- The Big Five Operators — C++ gives you five math operators: + (add), - (subtract), * (multiply), / (divide), and % (modulo/remainder). These are the same symbols you see on a calculator, except % which is brand new! You will use these in almost EVERY competitive programming problem, so knowing them well is super important.
- Addition and Subtraction — Easy Peasy — The + and - operators work exactly like you learned in math class. 3 + 5 gives 8. 10 - 4 gives 6. You can chain them: 1 + 2 + 3 + 4 equals 10. You can use them with variables too: if a = 5 and b = 3, then a + b is 8 and a - b is 2. Nothing surprising here!
- Multiplication — The Star Operator — In code, we use * for multiplication (not × like in math class). So 4 * 3 gives 12. Why a star? Because × looks too much like the letter x, and we use x as a variable name all the time! This is one of those things that confuses beginners, but you will get used to it fast.
- Integer Division — The Sneaky One — Here is the BIGGEST trap for beginners: when you divide two integers (whole numbers), C++ throws away the decimal part! So 5 / 2 gives 2, NOT 2.5. It is like asking 'how many whole times does 2 fit into 5?' — the answer is 2 times. The leftover just vanishes. This catches SO many people in CP contests!
- Real Division — Getting Decimals — If you WANT decimal results, at least one number must be a double (decimal type). So 5.0 / 2 gives 2.5, and 5 / 2.0 also gives 2.5. You can also cast: (double)5 / 2 gives 2.5. In CP, most problems use integer division, but when they ask for decimal output, remember this trick!
- Modulo % — The Remainder Superstar — The % operator gives you the REMAINDER after division. 7 % 3 = 1 (because 7 ÷ 3 = 2 remainder 1). 10 % 5 = 0 (divides evenly, no remainder). This is SUPER important in CP! You use it to check if a number is even (n % 2 == 0), to keep numbers in a range, and tons of problems say 'print the answer modulo 1000000007'. You will see % everywhere!
- Order of Operations — PEMDAS — Just like in math class, C++ follows order of operations: first Parentheses, then Multiplication/Division/Modulo (left to right), then Addition/Subtraction (left to right). So 2 + 3 * 4 = 14 (not 20). If you want 2 + 3 first, use parentheses: (2 + 3) * 4 = 20. When in doubt, always add parentheses — they make your code clearer AND safer!
- Shorthand Operators — Writing Less — C++ has shortcuts: a += 5 means a = a + 5. Similarly a -= 3, a *= 2, a /= 4, a %= 7. There is also a++ (add 1) and a-- (subtract 1). These save typing and are used everywhere in CP. When you see sum += x in a loop, it means 'keep adding x to the running total.'
Code example
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 17, b = 5;
cout << a + b << "\n"; // 22
cout << a - b << "\n"; // 12
cout << a * b << "\n"; // 85
cout << a / b << "\n"; // 3 (integer division!)
cout << a % b << "\n"; // 2 (remainder)
// Real division with decimals
double result = (double)a / b;
cout << fixed << setprecision(2) << result << "\n"; // 3.40
// Check if a number is even or odd
int n;
cin >> n;
if (n % 2 == 0) {
cout << "Even\n";
} else {
cout << "Odd\n";
}
// Sum of numbers using shorthand
int sum = 0;
int count;
cin >> count;
for (int i = 0; i < count; i++) {
int x;
cin >> x;
sum += x; // same as sum = sum + x
}
cout << "Sum = " << sum << "\n";
return 0;
}Line-by-line walkthrough
- 1. int a = 17, b = 5; — we create two variables and give them values. We picked 17 and 5 because they show interesting results for all operators.
- 2. cout << a + b; — prints 22. Addition works exactly like math class. 17 + 5 = 22.
- 3. cout << a - b; — prints 12. Subtraction is straightforward. 17 - 5 = 12.
- 4. cout << a * b; — prints 85. The star * means multiply. 17 × 5 = 85.
- 5. cout << a / b; — prints 3, NOT 3.4! This is integer division. 17 ÷ 5 = 3 with remainder 2. C++ only keeps the 3 and throws away the .4 part.
- 6. cout << a % b; — prints 2. This is the remainder. 17 ÷ 5 = 3 remainder 2. The % operator gives us that remainder.
- 7. double result = (double)a / b; — the (double) forces a to be treated as a decimal number. Now it is 17.0 / 5, which gives 3.4 instead of 3.
- 8. cout << fixed << setprecision(2) << result; — prints 3.40 with exactly 2 decimal places.
- 9. if (n % 2 == 0) — this checks if n is even! If you divide n by 2 and the remainder is 0, the number is even. This is one of the most common uses of modulo.
- 10. sum += x; — this is shorthand for sum = sum + x. Each time through the loop, we add the new number to our running total.
Spot the bug
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 5, b = 2;
double average = a + b / 2;
cout << "Average = " << average << "\n";
return 0;
}Need a hint?
Think about order of operations AND integer division. What happens to b/2 before it gets added to a?
Show answer
There are TWO bugs! First, b/2 is integer division, so 2/2 = 1 (correct here, but risky). Second, due to order of operations, b/2 happens BEFORE adding a, so you get 5 + 1 = 6, not the average of a and b. The fix is: double average = (double)(a + b) / 2; — this adds first, then divides, and the cast ensures decimal division. Result: 3.5.
Explain like I'm 5
Imagine you have a bag of 7 cookies and you want to share them equally among 3 friends. Division (7/3 = 2) tells you each friend gets 2 cookies. Modulo (7%3 = 1) tells you there is 1 cookie left over! That is literally what / and % do — split things up and tell you the leftover.
Fun fact
The modulo operator % is so important in competitive programming that there is a legendary number: 1000000007 (10^9 + 7). It is a prime number, and hundreds of CP problems ask you to print your answer modulo this number. Why? Because answers can get astronomically huge, and taking modulo keeps them manageable. CP contestants call it 'MOD' and have it memorized!
Hands-on challenge
Write a program that reads two integers a and b, then prints five lines: a+b, a-b, a*b, a/b (integer division), and a%b. Then try it with a=17, b=5 and verify you get 22, 12, 85, 3, 2. Bonus: also print the result of (double)a/b with 2 decimal places.
More resources
- Codeforces — Watermelon (uses modulo!) (Codeforces)
- C++ Arithmetic Operators Explained (YouTube)
- USACO Guide — Basic Math Operations (USACO Guide)