c语言

c语言平方根函数如何使用

小亿
205
2024-01-30 14:54:55
栏目: 编程语言

C语言中求平方根的函数是sqrt()函数,它定义在math.h头文件中。

使用方法如下:

  1. 在程序中引入math.h头文件:#include <math.h>

  2. 在需要使用平方根函数的地方调用sqrt()函数,并将需要求平方根的数作为参数传递给该函数,如:

    double result = sqrt(16); // 求16的平方根,结果赋值给result变量

  3. sqrt()函数返回一个double类型的结果,因此需要用一个double类型的变量来接收该结果。

注意事项:

示例代码:

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

int main() { double result = sqrt(16); printf(“The square root of 16 is %f\n”, result); return 0; }

输出结果: The square root of 16 is 4.000000

0
看了该问题的人还看了