English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and examples of sqrt() function

C++ Library Function <cmath>

C ++The sqrt() function in it returns the square root of the number.

 √x = sqrt(x)

This function is in<cmath>defined in the header file.

The prototype of sqrt() [from C ++ 11[Standard library begins]

double sqrt(double x);
float sqrt(float x);
long double sqrt(long double x);
double sqrt(T x); //is an integer type

The sqrt() parameter

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

An error will occur if a negative parameter is passed to the sqrt() function.

The return value of sqrt()

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

Example1How sqrt() works in C ++How does the work in Chinese?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double x = 10.25, result;
	result = sqrt(x);
	cout << "10.25The square root is " << result << endl;
	return 0;
}

The output when running the program is:

10.25The square root is 3.20156

Example2:sqrt() function with integer parameter

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	long x = 464453422;
	double result = sqrt(x);
	cout << "464453422The square root is " << result << endl;
	return 0;
}

The output when running the program is:

464453422The square root is 21551.2

 C++ Library Function <cmath>