Join our newsletter for the latest updates.

C sqrt()

The sqrt() function computes the square root of a number.

Function prototype of sqrt()

double sqrt(double arg);

The sqrt() function takes a single argument (in double) and returns its square root (also in double).

[Mathematics]  √x = sqrt(x) [In C Programming]

The sqrt() function is defined in math.h header file.


To find the square root of int, float or long double data types, you can explicitly convert the type to double using cast operator.

int x = 0;
double result;
result = sqrt(double(x));

You can also use the sqrtf() function to work specifically with float and sqrtl() to work with long double type.

long double sqrtl(long double arg );
float sqrtf(float arg );

Example: C sqrt() Function

#include <math.h>
#include <stdio.h>

int main() {
   double number, squareRoot;

   printf("Enter a number: ");
   scanf("%lf", &number);

   // computing the square root
   squareRoot = sqrt(number);

   printf("Square root of %.2lf =  %.2lf", number, squareRoot);

   return 0;
}

Output

Enter a number: 23.4
Square root of 23.40 =  4.84
Did you find this article helpful?