C语言编程,分别用while语句、do-while语句、for语句编写程序,计算e≈1+1/1!+ 5
C语言编程,分别用while语句、do-while语句、for语句编写程序,计算e≈1+1/1!+1/2!+…+1/n!...
C语言编程,分别用while语句、do-while语句、for语句编写程序,计算e≈1+1/1!+1/2!+…+1/n!
展开
2个回答
展开全部
#include <stdio.h>
double exp1(int n) {
double i,m = 1,ee = 1;
for(i = 1; i < n; ++i) {
m *= i;
ee += 1.0/m;
}
return ee;
}
double exp2(int n) {
double i = 1,m = 1,ee = 1;
while(i < n) {
m *= i;
ee += 1.0/m;
++i;
}
return ee;
}
double exp3(int n) {
double i = 1,m = 1,ee = 1;
do {
m *= i;
ee += 1.0/m;
++i;
}while(i < n);
return ee;
}
int main() {
int n;
printf("n = ");
scanf("%d",&n);
printf("for: %lf\n",exp1(n));
printf("while(): %lf\n",exp2(n));
printf("do_while(): %lf\n",exp3(n));
return 0;
}
展开全部
#include "stdio.h"
main()
{ int n, k;
double e, t, p;
e=1.0; n=1;
do //do while
{
p=1.0;
for(k=1;k<=n; k++)
p=p*k;
t=1.0/p;
e=e+t;
n++;} while(t>=1e-6);
printf("e=%lf \n", e);
}
#include<stdio.h>
main()
{
int i,j;
long p;
double e ;
for(i=1,e=1;;i++)
{
for(j=1,p=1;j<=i;j++)
p=p*j;
e=e+1.0/p;
if(1.0/p < 1e-6)
break;
}
printf("e=%lf",e);
}
#include "stdio.h"
main()
{ int n, k;
double e, t=1, p;
e=1.0; n=1;
while(t >= 1e-6) //while
{
p=1.0;
for(k=1;k<=n; k++)
p=p*k;
t=1.0/p;
e=e+t;
n++;}
printf("e=%lf \n", e);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询