我在计算p1=(1/1)*(1/2)*...*(1/n)但有些地方不对劲printf给我0.000...0#include <stdio.h>int main(void) {
int i,num;
float p3;
do {
printf ("give number N>3 : \n" );
scanf( "%d", &num );
} while( num <= 3 );
i = 1;
p3 = 1;
do {
p3=p3*(1/i);
printf( "%f\n",p3 );
} while ( i <= num );
printf("\nP3=%f",p3);
return 0;}
3 回答
慕容3067478
TA贡献1773条经验 获得超3个赞
1
i
1/i
float
double
):
p3 *= 1. / i;
PIPIONE
TA贡献1829条经验 获得超9个赞
当您想要从两个整数获得浮点数输出时,需要将其中一个转换为浮点数。 int c = 15; int b = 8; printf("result is float %f\n", c / (float) b); // result is float 1.875000 printf("result is float %f\n", (float) c / b); // result is float 1.875000
- 3 回答
- 0 关注
- 472 浏览
添加回答
举报
0/150
提交
取消