C++ Standard Library C++ STL Library

C++ <cmath> - sqrt() Function



The C++ <cmath> sqrt() function returns the square root of the given number.

Syntax

double sqrt (double x);
float sqrt (float x);
long double sqrt (long double x);
double sqrt (double x);
float sqrt (float x);
long double sqrt (long double x);
//additional overloads for integral types
double sqrt (T x);                    

Parameters

x Specify a positive number.

Return Value

Returns the square root of the specified number.
If the x is negative, domain error occurs.

Example:

In the example below, sqrt() function is used to find out the square root of the given number.

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  cout<<sqrt(25)<<"\n";
  cout<<sqrt(30)<<"\n";
  cout<<sqrt(35.5)<<"\n";
  cout<<sqrt(100)<<"\n";  
  return 0;
}

The output of the above code will be:

5
5.47723
5.95819
10

❮ C++ <cmath> Library

5