sqrt()

Christine_Yang's avatar
Published Sep 8, 2022Updated Dec 21, 2022
Contribute to Docs

The sqrt() function returns the square root of the argument.

Syntax

sqrt(x)

If the parameter x is negative (less than 0), then a domain error will occur.

Example

The following example uses the sqrt() function to find the square root of 4:

#include <iostream>
#include <cmath>
int main() {
double x = 4;
double result;
result = std::sqrt(x);
std::cout << "The square root of " << x << " is " << result << "\n";
// Output: The square root of 4 is 2
}

Codebyte Example

The following example is runnable and returns the square root of 9 with the sqrt() function:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy