C++ Operators & Math

Arithmetic and the cmath library.

✍️ 2 exercises in this lesson

C++ has the same operators as C: arithmetic + - * / % ++ --, assignment, comparison and logical operators.

<cmath> provides sqrt(), pow(), round(), ceil(), floor(), abs(); std::max() and std::min() are in <algorithm>.

Key Takeaways: C++ Operators & Math

Mastering C++ Operators & Math helps you write clean and reliable C++ code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C++ Operators & Math Code Example

C++
#include <iostream>
#include <cmath>
#include <algorithm>

int main() {
    std::cout << 17 / 5 << " " << 17 % 5 << " " << 17 / 5.0 << "\n";
    std::cout << std::sqrt(144) << " " << std::pow(2, 8) << "\n";
    std::cout << std::round(2.5) << " " << std::ceil(2.1) << "\n";
    std::cout << std::max(7, 3) << "\n";
    return 0;
}
Output
3 2 3.4
12 256
3 3
7
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C++ Operators & Math Practice Exercises

Check what you learned in “C++ Operators & Math”.

/ 2 solved
Fill in the blank

Get 2 to the power of 8.

std::(2, 8)
✓ Correct! Not quite. Try again.
Multiple choice

Which header contains sqrt() and pow()?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services