c语言中算数平方根怎么表示

在C语言中,求一个数字的算术平方根可以使用 sqrt() 函数。该函数在 math.h 头文件中定义。下面是使用 sqrt() 函数求一个数字的算术平方根的示例代码:

#include <stdio.h>
#include <math.h>
int main() {
    double n;
    printf("请输入一个数字n:");
    scanf("%lf", &n);
    if (n < 0) { // 负数没有算数平方根
        printf("输入的数字不合法\n");
        return 0;
    }
    double result = sqrt(n);
    printf("%f的算数平方根是%f\n", n, result);
    return 0;
}

在上面的示例代码中,我们首先通过 scanf() 函数读入一个数字 n,然后判断 n 是否小于0。如果小于0,则说明该数字没有算数平方根,直接输出提示信息;否则,使用 sqrt() 函数求出 n 的算数平方根,将结果保存到 double 类型的变量 result 中,最后使用 printf() 函数输出结果。

使用 sqrt() 函数时,需要在代码文件的开头添加 #include <math.h> 头文件。此外,使用 sqrt() 函数时,需要将待求平方根的值作为函数的参数传入,函数返回值为求得的平方根。

 
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定