用c语言求x的n次方,要求用函数
#include <stdio.h>double power(double x,int n){
double tmp = 1.0;
while (n-- > 0)
tmp *= x;
return tmp;
}
int main(void){
double a =0.00;
intb = 0;
scanf("%f",&a);
scanf("%d",&b);
power(a,b);
printf("%d",power(a,b));
return 0;
}
=============================运行结果=======================
4.3
3
0 你用%d去显示double怎么可能成功
double用%f也是不对的 #include <stdio.h>
double power(double x,int n){
double temp =x;
while( --n>0)
x *= temp;
return x;
}
int main(void){
double a;
intb;
scanf("%lf",&a);
scanf("%d",&b);
printf("%lf",power(a,b));
return 0;
} 万千只cnm 发表于 2021-6-5 16:59
类型和格式要一致 #include <stdio.h>
double power( double x, int n )
{
double num=1;//注意为double型
for(int i=1;i<=n;i++)
{
num=x*num;
}
return num;
}
int main()
{
double x;
int n;
scanf("%lf %d", &x, &n);
printf("%f\n", power(x, n));
return 0;
} //定义pow函数(求x的y次方幂)
#include <stdio.h>
void main()
{
double pow(double,double); //改变数值
double x=2.0,y=4.0,z;
z=pow(x,y);
printf("%f的%f次方是%f\n",x,y,z);
}
double pow(double x,double y)
{
double result;
int i,k;
for(i=1,result=1;i<=y;i++)
{
result=result*x;
}
return(result);
}
页:
[1]