sqrt()

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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C++ on Codecademy