sqrt() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

sqrt() in C++ is used to return the square root of any number. It is defined in the cmath header file. It takes in a non-negative number as an argument and returns the square root of that number.

We can state mathematically : sqrt(x) = √x.

Syntax of sqrt() in C++

The syntax of sqrt in C++ is as follows:

Here x is a non-negative number whose square root in C++ is to be calculated.

Parameters of sqrt() in C++

Following are the parameters of sqrt() function in C++ :

  • x : A non-negative number whose square root is to be calculated.

Return Value of sqrt() in C++

The return value of sqrt() in C++ is the square root of the given number x. This square root returned is of datatype double.

Exceptions for sqrt() in C++

Let us now go through the exceptions of sqrt() in C++ :

  • sqrt() in C++ is only defined for non-negative numbers. This means negative numbers are out of the domain for the sqrt() method. Thus, domain error will occur if negative numbers are passed as arguments to sqrt in C++.
  • If we use the sqrt() method in C++ without any argument, it will throw an error stating that "no matching function for call to sqrt()"

Example of sqrt() Function

Let us now go through an example and the code for sqrt() in C++:

We will see what sqrt() in C++ returns if we pass zero, a perfect square number, a not-perfect number, a decimal number, and a negative number as a parameter.

Code :

Output :

As we can see, we get the square root of the number whenever we pass a non-negative argument. The square root is not defined for negative numbers, so we get nan when we pass -19.

What is sqrt() in C++ ?

Let us once again revisit the points we studied about sqrt in C++ :

  • The sqrt() function in C++ is defined in the cmath header file and is used to return the square root of a number.
  • The sqrt() method takes a non-negative number as an argument and returns its square root in datatype double.
  • sqrt() in C++ is not defined for negative numbers. Thus, it gives domain error whenever negative numbers are passed as an argument.
  • sqrt() method can not be called without using any argument, else it will throw an error.
  • sqrt() in C++ is defined for double values only, but it works fine with integral values, too, since the integer values are implicitly converted into type double.

square root of x

Example :

In the first example, we shall use sqrt with double arguments, and in the second, we shall use sqrt with integral arguments.

1. Sqrt in c++

Let us calculate the sqrt of a double value, say 98.56:

Output:

Explanation:
As we can see, the return value is of type double.

2. Sqrt in c++ with Integral Argument

Let us see what happens if we pass an integral value, say 98, to the sqrt() method.

Output:

Explanation:
Though sqrt() in C++ is defined for double only, it did not through an error when we passed an integral value. This is because C++ implicitly converted the integral value to a double value.

The return value is of type double.

Take the first step towards becoming a C++ expert. Enroll in our free C++ course and bring your software ideas to life.

Conclusion

  • sqrt() in C++ returns the square root of a non-negative number. It gives a domain error if negative numbers are passed.
  • It is defined in the cmath header file.
  • The return value is of type double.