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

The sqrt() function in C returns the square root of the number. It is defined in the math.h header file.

Syntax for sqrt() in C

The syntax for the sqrt function in C is given below:

Parameters for sqrt() in C

The sqrt() function takes a single number(in double) as the input.

Return Values for sqrt() in C

The sqrt() function returns a double number value as the output. However, if the input is negative, it returns -nan. To understand why that happens, see Exceptions.

Exceptions for sqrt() in C

When the input number is negative, the sqrt function in C returns -nan(not a number). This is because the square root of a negative number is imaginary, and the sqrt() function doesn't support an imaginary number. This is a domain error, i.e., the input is not within the function's domain.

Output

Example to Illustrate the Working of sqrt() Function in C

Output

What is sqrt() Function in C?

The sqrt() function in C returns the square root of the number. It is defined in the math.h header file. It takes the input(in double) and returns the output(in double). If the input is negative, it returns a domain error-nan.

How to Use the sqrt() Function in C?

  • If we want to use the sqrt function in C for int, float, etc., we can typecast these into double values and then pass them into the function.
  • If the input is not a double value (e.g., int, float, etc.), it is automatically typecasted into a double. The sqrt() function in C is defined such that if the input is not a double value(e.g., int, float, etc.), it is typecasted into double, and then its square root is calculated.
  • We can also use the sqrtf() for float values and sqrtl() for long double values.

Note: Since the sqrt() function returns double values, it is also prone to small roundoff errors.

More Examples of sqrt() in C

  • Program to get the square root of a number using the sqrt() function in C.

Output

Explanation The program declares three variables a,b, and c and computes the square root of these numbers using the sqrt() function. Note that variables a and b are integers. When they are passed into the sqrt() function, it typecasts them into a double value.

  • Program to take a number from the user and get its square root using the sqrt() function in C.

Output

Explanation

In this program, we take a non-negative number from the user and compute its square root using the sqrt() function. If the user passes a negative number, it will result in a domain error. See Exception for more details.

  • Program to get the square root of a number using the pow() function.

Output

Explanation

In this program, we calculate the square root of a number using the pow() function. The pow() function takes two parameters, the base, and the exponent. Therefore, when we compute pow(a, 0.5), it is the same as the square root of a.

  • Program to get the square root of a number using a user-defined function without using the sqrt() function.

Output

Explanation

In this program, we write a user-defined function to calculate the square root of the number. The user enters a number, a, and we pass this number to the function squareRoot(). We store a / 2 in j. Initially, i = 0. We then assign j to i and update the value of j. This goes on until j is not equal to i. This method is called the Newton Raphson Method.

Conclusion

  • The sqrt function in C returns the square root of the number.
  • It takes a single, double parameter and returns the output as a double.
  • It returns a domain error if the input is negative.

See Also

Some functions similar to the sqrt function are as follows.