C++ Math

C++ gives you a lot of ways to work with numbers. Some math tools are built right into the language, while others live in a standard header called <cmath>. In this lesson you will learn how to do basic calculations, find the smallest or largest of two values, and use handy math functions like square root, power, and rounding.

Every program eventually needs to do a little math, whether it is adding up a bill, measuring distance, or rounding a price. C++ handles simple arithmetic with the ordinary operators you already know, and it provides extra functions for anything more advanced.

Basic arithmetic

You do not need any special library for the everyday operations. The operators + (add), - (subtract), * (multiply), / (divide), and % (remainder) are part of the core language and work on numbers directly.

Simple calculations

#include <iostream>
using namespace std;

int main() {
  int a = 20;
  int b = 6;

  cout << "Sum: " << a + b << "\n";        // 26
  cout << "Product: " << a * b << "\n";    // 120
  cout << "Quotient: " << a / b << "\n";   // 3 (integer division)
  cout << "Remainder: " << a % b << "\n";  // 2
  return 0;
}
Note: When you divide two integers, C++ throws away the fractional part. 20 / 6 becomes 3, not 3.33. If you need a decimal answer, make at least one value a double, for example 20.0 / 6.

min() and max()

Two of the most useful math helpers are max() and min(). They return the larger or smaller of two values, which saves you from writing an if statement every time. Both are available through the <algorithm> header.

Finding the highest and lowest value

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

int main() {
  int score1 = 78;
  int score2 = 91;

  cout << "Best score: " << max(score1, score2) << "\n";  // 91
  cout << "Worst score: " << min(score1, score2) << "\n"; // 78
  return 0;
}

The <cmath> library

For more advanced math, include the <cmath> header. It unlocks functions such as sqrt() for square roots, pow() for exponents, round() for rounding to the nearest whole number, and abs() for the absolute value. These functions usually return a double.

Using math functions

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

int main() {
  cout << sqrt(64) << "\n";      // 8
  cout << pow(2, 10) << "\n";    // 1024 (2 to the power of 10)
  cout << round(4.7) << "\n";    // 5
  cout << abs(-15) << "\n";      // 15
  return 0;
}

Common <cmath> functions

FunctionDescriptionExample
sqrt(x)Square root of xsqrt(25) returns 5
pow(x, y)x raised to the power ypow(3, 2) returns 9
round(x)Rounds x to the nearest integerround(2.4) returns 2
ceil(x)Rounds x upceil(2.1) returns 3
floor(x)Rounds x downfloor(2.9) returns 2
abs(x)Absolute (positive) value of xabs(-8) returns 8
Note: Remember to add #include <cmath> at the top of your file before using functions like sqrt() or pow(), otherwise the compiler will not recognize them.

Exercise: C++ Math

Which header must be included to use functions like sqrt() and pow() in standard C++?