Lesson 6 of 50 beginner

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

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. 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. 2. cout << a + b; — prints 22. Addition works exactly like math class. 17 + 5 = 22.
  3. 3. cout << a - b; — prints 12. Subtraction is straightforward. 17 - 5 = 12.
  4. 4. cout << a * b; — prints 85. The star * means multiply. 17 × 5 = 85.
  5. 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. 6. cout << a % b; — prints 2. This is the remainder. 17 ÷ 5 = 3 remainder 2. The % operator gives us that remainder.
  7. 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. 8. cout << fixed << setprecision(2) << result; — prints 3.40 with exactly 2 decimal places.
  9. 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. 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

Open interactive version (quiz + challenge) ← Back to course: CP Zero to Hero