sqrt()

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...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C++ on Codecademy