sqrt() returns the non-negative square root of a floating-point value. For ordinary real-number calculations, the argument must be zero or positive.
double sqrt(double x);
#include <math.h>
#include <stdio.h>
int main(void) {
double value = 144.0;
printf("Square root: %.2f\n", sqrt(value));
return 0;
}
Output
Square root: 12.00
#include <math.h>
#include <stdio.h>
int main(void) {
double value;
printf("Enter a non-negative number: ");
if (scanf("%lf", &value) != 1) {
printf("Invalid input.\n");
return 1;
}
if (value < 0.0) {
printf("No real square root for a negative value.\n");
return 1;
}
printf("sqrt = %.6f\n", sqrt(value));
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.