我编码了以下内容:
#include<stdio.h>
#include<math.h>
void main()
{
float x,p;
int i,n,f=1;
printf("Enter the value of x,n\n");
scanf("%d %d",&x,&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
f=f*i;
}
p=(float)pow(x,n)/f;
printf("The value of p is %.3f",p);
}
if(n==0)
{
p=(float)pow(x,n)/1;
printf("The value of p is %d",p);
}
getch();
}
但是运行不正常。我在哪里弄错了?
scanf("%d %d",&x,&n);
应更改为scanf("%f %d",&x,&n);
因为x
是`float type @dragosht提到过它。printf("The value of p is %d",p);
应该正确,因为printf("The value of p is %f",p);
因为p
也是float
类型。p = 0;
,因为您没有使用键盘将值分配给p
。在那里,有时您会因此而损坏值。