编写程序,求e的值。e≈1+1⼀1!+1⼀2!+1⼀3!+1⼀4!+···+1⼀n! (1)用for循环,计算前50项 (2)用while循环

2024-12-17 12:39:20
推荐回答(1个)
回答1:

(1)
#include

void main()
{
int n=1;
float i=1,e=1;
for(n=1;n<=50;n++)
{
i=i*n;
e=e+1/i;
}
printf("%f",e);
}
(2)
#include
void main()
{
int n=1;
float i=1,e=1;
while(1/i>10e-4)
{
i=i*n;
e=e+1/i;
n++;
}
printf("%f",e);
}