Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Math
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 12 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++BeginnerLesson 12 of 50CamboFreelanceJune 19, 2026

C++ Math

Explore the C++ cmath library. Learn sqrt, pow, abs, ceil, floor, round, and trigonometric functions with practical examples.


Tutorials › C++ › Math
Beginner6 min readLesson 12 of 50

In This Lesson

  • cmath Library
  • Common Functions
  • min and max
  • Quiz

The <cmath> Library

C++ provides powerful math functions through the <cmath> header. Include it to access square roots, powers, trigonometry, and more.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    cout << sqrt(25)       << endl;   // 5
    cout << pow(2, 10)     << endl;   // 1024
    cout << abs(-15)       << endl;   // 15
    cout << ceil(4.2)      << endl;   // 5
    cout << floor(4.9)     << endl;   // 4
    cout << round(4.5)     << endl;   // 5
    cout << log(M_E)       << endl;   // 1 (natural log)
    cout << log10(1000)    << endl;   // 3
    return 0;
}

Output:

5
1024
15
5
4
5
1
3

Common Math Functions

FunctionDescriptionExample
sqrt(x)Square rootsqrt(9) → 3
pow(x, y)x to the power ypow(2,8) → 256
abs(x)Absolute valueabs(-7) → 7
ceil(x)Round upceil(3.1) → 4
floor(x)Round downfloor(3.9) → 3
round(x)Round to nearestround(3.5) → 4
sin/cos/tanTrigonometry (radians)sin(M_PI/2) → 1

max() and min()

These functions are available without any extra header:

cout << max(10, 20);   // 20
cout << min(10, 20);   // 10
cout << max(3.5, 2.1); // 3.5

Exercise

Write a program that reads a number from the user and prints its square root, square (power of 2), and absolute value.

Show Solution
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double n;
    cout << "Enter a number: "; cin >> n;
    cout << "Square root: " << sqrt(n)    << endl;
    cout << "Squared: "     << pow(n, 2)  << endl;
    cout << "Absolute: "    << abs(n)     << endl;
    return 0;
}

Quiz

  1. Which header provides sqrt() and pow()?

    • A) <math>
    • B) <cmath>
    • C> <algorithm>
    • D) <numbers>
    Answer

    B) <cmath>

  2. What does ceil(4.1) return?

    • A) 4
    • B) 4.1
    • C) 5
    • D) 4.0
    Answer

    C) 5 — ceil always rounds up to the next whole number.

Summary

  • Include <cmath> for math functions.
  • Key functions: sqrt, pow, abs, ceil, floor, round.
  • max() and min() work without extra headers.
← Previous: Strings Next: C++ Booleans →
c++mathcmathbeginner
PreviousLesson 11: C++ StringsNextLesson 13: C++ Booleans
Back to All Tutorials