C语言中计算一个数的N次方可以用库函数pow来实现。函数原型:double pow(double x, double y)。
代码如下:
#include <stdio.h>
#include <math.h>
int main( )
{
printf("%f",pow(x,y));
return 0;
}
注:使用pow函数时,需要将头文件#include<math.h>包含进源文件中。、
扩展资料:
其他方法表示一个数的n次方:
#include <stdio.h>
int main( )
{ int i,k = n; for(i = 1;i <n;i++)
{ k *= 2;
}
printf("%d",k);
return 0;
}
有两个函数可以实现,double pow(double x, double y),double pow10(int p)下面是这两个函数的使用方法,个人建议用:pow10(n)
函数名: pow
功 能: 指数函数(x的y次方)
用 法: double pow(double x, double y)
程序例:
#include <math.h>
#include <stdio.h>
int main(void)
{
double x = 2.0, y = 3.0
printf("%lf raised to %lf is %lf\n", x, y, pow(x, y))
return 0
}
函数名: pow10
功 能: 指数函数(10的p次方)
用 法: double pow10(int p)
程序例:
#include <math.h>
#include <stdio.h>
int main(void)
{
double p = 3.0
printf("Ten raised to %lf is %lf\n", p, pow10(p))
return 0
}