Skip to main content

Sqrt() function in C

 

What is the sqrt() function in C?

In C programming, the sqrt() function is used to find out the square root of a number. sqrt() function is one of the pre-defined functions in C programming, stored in math.h header file. We can use the sqrt() function, after including the math. h header file at the start of our program.
There are many other library functions and header files available in the C programming language, you can study them on our library functions on the C page

 

How to use sqrt()

sqrt() function in C programming is a double-type function, that is it returns the value in decimal format. This function works with int, short, byte long, double, and float datatypes, but sometimes, on some compilers, it may give an error while using any datatype other than double.

Syntax for sqrt() function

  • sqrt( integer );

Let’s take a sample code, and understand how to use the sqrt() function

C code for using sqrt() function

#include 
#include 
int main()
{
    double ans=0.0;
    int a = 25;
    ans= sqrt(a);
    
    printf("The square root of %d is %f",a,ans);
 
    return 0;
}

Output

The square root of 25 is 5.000000

 

Comments