w3resource

C sqrt() function

C sqrt() function - Calculate Square Root

Syntax:

double sqrt(double x)

The sqrt() function is used to calculate the nonnegative value of the square root of x.

Parameters:

Name Description Required /Optional
x Non-negative floating-point value Required

Return value from sqrt()

  • Returns the square root result.
  • Returns 0 if x is negative.

Example: sqrt() function

The following example shows the usage of sqrt() function.


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

int main( void )
{
   double val = 66.34, result;
   result = sqrt( val );
   if( val < 0 )
      printf( "Error: sqrt returns %f\n", result );
   else
      printf( "The square root of %.2f is %.2f\n", val, result );
      
   val = -100;
   result = sqrt( val );
   if( val < 0 )
      printf( "\nError: sqrt returns %f\n", result );
   else
      printf( "\nThe square root of %.2f is %.2f\n", val, result );
}

Output:

The square root of 66.34 is 8.14

Error: sqrt returns -1.#IND00

C Programming Code Editor:

Previous C Programming: C pow()
Next C Programming: C ceil()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/math/c-sqrt.php