C++ provides many useful math functions in the <cmath>
library. Here are some commonly used ones:
pow(x, y)
- raises x to the power of ysqrt(x)
- square root of xabs(x)
- absolute value of xround(x)
- rounds x to the nearest integerceil(x)
- rounds x up to the nearest integerfloor(x)
- rounds x down to the nearest integer#include <cmath>
double result = pow(2, 3); // 8.0 (2^3)
double root = sqrt(16); // 4.0
double rounded = round(3.7); // 4.0
double ceiling = ceil(3.2); // 4.0
double floor_val = floor(3.8); // 3.0
Note: These functions return double
values, so you might need to cast them if you want integers.
Calculate the square root of 25 and store it in a variable called squareRoot
. Use the sqrt()
function from the <cmath>
library.
Click "Check Answer" to run your code.