小弟编程菜鸟,大哥求救

来源:百度知道 编辑:UC知道 时间:2024/07/01 06:49:29
下面是程序,应当出来2的1次方是2,到2的10次方是1024;结果却不对,望大哥指点

#include<stdio.h>
#include<math.h>
int main()
{
int i=1;
do
{
printf("2 to the power of %d is %d\n",i,pow(2,i));
i++;
}
while(i<=10);
return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
int i=1;
do
{
printf("2 to the power of %d is %d\n",i,pow(2,i));
i++;
}
while(i<=10);
return 0;
}
功 能: 指数函数(x的y次方)
用 法: double pow(double x, double y);
pow 返回是浮点类型的

#include<stdio.h>
#include<math.h>
int main()
{
int i=1;
do
{
printf("2 to the power of %d is %f\n",i,pow(2,i));
i++;
}

while(i<=10);
return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
int i=1;
do
{
printf("2 to the power of %d is %f\n",i,pow(2,i));
i++;
}
while(i<=10);
return 0;
}

上面第二个%d改为%f

即可

pow(2, i)的返回值是浮点数啊

换成下面这句试试
printf("2 to the power of %d is %g\n",i,pow(2,i));