温馨提示×

c语言怎么进行幂次方计算

小亿
126
2024-05-27 14:26:08
栏目: 编程语言

在C语言中,可以使用数学库中的pow函数来进行幂次方计算。pow函数的原型如下:

double pow(double x, double y);

其中x为底数,y为指数,函数返回x的y次方。例如,计算2的3次方可以使用如下代码:

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

int main() {
    double base = 2;
    double exponent = 3;
    double result = pow(base, exponent);
    
    printf("%.2f raised to the power of %.2f is %.2f\n", base, exponent, result);
    
    return 0;
}

运行以上代码,输出结果为:

2.00 raised to the power of 3.00 is 8.00

0