C++ sqrt()

The sqrt() function in C++ returns the square root of a number.
[Mathematics]  √x = sqrt(x) [In C Programming]

This function is defined in <cmath> header file.


sqrt() prototype [As of C++ 11 standard]

double sqrt(double x);
float sqrt(float x);
long double sqrt(long double x);
double sqrt(T x); // For integral type

sqrt() Parameters

The sqrt() function takes a single non-negative argument.

If negative argument is passed to sqrt() function, domain error occurs.


sqrt() Return value

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


Example 1: How sqrt() works in C++?

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double x = 10.25, result;
	result = sqrt(x);
	cout << "Square root of " << x << " is " << result << endl;

	return 0;
}

When you run the program, the output will be:

Square root of 10.25 is 3.20156

Example 2: sqrt() function With integral Argument

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	long x = 464453422;
	double result = sqrt(x);
	cout << "Square root of " << x << " is " << result << endl;
	
	return 0;
}

When you run the program, the output will be:

Square root of 464453422 is 21551.2