Basics Lesson 5 of 17
C++ Operators and the cmath Library
Arithmetic, assignment, comparison and logical operators in C++, increment and decrement, plus sqrt, pow, abs, round and other functions from cmath.
C++ inherits all of C's operators, so if you know them from C, Java or JavaScript, you'll feel at home. This lesson reviews them with C++ examples and introduces the <cmath> library.
Arithmetic and assignment
#include <iostream>
using namespace std;
int main() {
int minutes = 135;
cout << minutes / 60 << " hr " << minutes % 60 << " min" << endl;
int score = 10;
score += 5;
score *= 2;
score--;
cout << "Score: " << score << endl;
int a = 5;
int b = a++; // b = 5, a = 6
int c = ++a; // a = 7, c = 7
cout << a << " " << b << " " << c << endl;
return 0;
}
Output
2 hr 15 min Score: 29 7 5 7
Comparison and logical operators
Comparisons give a bool. Logical operators combine them: && (and), || (or), ! (not). C++ also accepts the words and, or and not.
#include <iostream>
using namespace std;
int main() {
double average = 91.0;
int lowest = 86;
int age = 17;
cout << boolalpha;
cout << "With honors: " << (average >= 90 && lowest >= 85) << endl;
cout << "Minor or senior: " << (age < 18 || age >= 60) << endl;
cout << "Adult: " << !(age < 18) << endl;
cout << "Same with words: " << (average >= 90 and lowest >= 85) << endl;
return 0;
}
Output
With honors: true Minor or senior: true Adult: false Same with words: true
The cmath library
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << sqrt(144) << endl;
cout << pow(2, 10) << endl;
cout << abs(-7.5) << endl;
cout << round(89.5) << " " << ceil(4.1) << " " << floor(4.9) << endl;
cout << hypot(6, 8) << endl;
cout << max(12, 30) << " " << min(12, 30) << endl;
return 0;
}
Output
12 1024 7.5 90 5 4 10 30 12
| Function | Meaning |
|---|---|
sqrt(x) | Square root |
pow(a, b) | a to the power b |
abs(x) | Absolute value |
round, ceil, floor | Nearest, up, down |
hypot(a, b) | √(a² + b²) |
max(a, b), min(a, b) | Larger / smaller (from <algorithm>, usually available) |
Example: compound interest
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
double principal = 10000; // pesos
double rate = 0.04; // 4% per year
int years = 5;
double amount = principal * pow(1 + rate, years);
cout << fixed << setprecision(2);
cout << "After " << years << " years: P" << amount << endl;
cout << "Interest earned: P" << amount - principal << endl;
return 0;
}
Output
After 5 years: P12166.53 Interest earned: P2166.53
Random numbers (modern C++)
C++ has a powerful <random> library:
#include <iostream>
#include <random>
using namespace std;
int main() {
random_device rd;
mt19937 gen(rd()); // random number engine
uniform_int_distribution<int> dice(1, 6); // 1 to 6, both included
for (int i = 0; i < 5; i++) cout << dice(gen) << " ";
cout << endl;
return 0;
}
Output
4 1 6 6 2
Your numbers will differ. You may also see the older C way, rand() % 6 + 1 with srand(time(0)); it works for simple games but gives lower-quality randomness.
Operator precedence
Highest to lowest: (), unary ! ++ --, * / %, + -, << >>, comparisons, == !=, &&, ||, assignment. When unsure, use parentheses.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04