在C语言中,求平方根可以使用 sqrt()
函数。该函数在 math.h
头文件中定义。下面是使用 sqrt()
函数求平方根的示例代码:
#include <stdio.h> #include <math.h> int main() { double x = 16.0; double y = sqrt(x); printf("%f的平方根是%f\n", x, y); return 0; }
在上面的示例代码中,我们定义了一个 double
类型的变量 x
,并将其初始化为 16.0
,然后使用 sqrt()
函数求出 x
的平方根,将结果保存到 double
类型的变量 y
中,最后使用 printf()
函数输出结果。 需要注意的是,使用 sqrt()
函数时,需要在代码文件的开头添加 #include <math.h>
头文件。此外,使用 sqrt()
函数时,需要将待求平方根的值作为函数的参数传入,函数返回值为求得的平方根。
评论