作业帮 > 综合 > 作业

用pow函数求a的3次方(a为实数)

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/10/02 02:40:57
用pow函数求a的3次方(a为实数)
以下这段代码为什么不对?
#include
#include
float pow(float x,int y);
int main()
{
float a,product;
printf("please input value of a:");
scanf("%f",&a);
product=pow(a,3);
printf("\ncube of %.4f is%.4f\n\n",product);
system("PAUSE");
return 0;
}
float pow(float x,int y)
{
int p;
for(p=1;y>0;y--)
p*=x;
return(p);
}
用pow函数求a的3次方(a为实数)
#include
#include
float pow(float x,int y);
int main()
{
float a,product;

printf("please input value of a:");
scanf("%f",&a);

product=pow(a,3);

printf("\ncube of %.4f is%.4f\n\n",a,product); //这里缺少一个 a

system("PAUSE");
return 0;
}
float pow(float x,int y)
{
int p;

for(p=1;y>0;y--)
p*=x;

return((float)p); //这里强制转换一下就可以了
}