The sqrt()
function in C++ is used to return the square root of a given number.
The sqrt()
function is defined in the <cmath>
header file.
sqrt(double num);
The sqrt()
function takes a non-negative number as a parameter.
Note: The domain error occurs when a negative number is passed as an argument to the
sqrt()
function.
The sqrt()
function returns the square root of the argument passed to it.
#include <iostream>#include <cmath>using namespace std;int main() {// creating a variabledouble x = 25;// using the sqrt() functiondouble result = sqrt(x);cout << "Square root of " << x << " is " << result;return 0;}
x
.sqrt()
function to calculate the square root of variable x
and assign the return value to the variable result
.result
.