#include
int main(){
float p=1,a=1,e=1;
while(1/a>=1e-6){
a*=p;
e+=1/a;
p++;}
printf("e=%f\n",e);
return 0;
}
代码如下
#include
#include
int main(){
double e;
double item = 1;
int t = 1;
double sum = 0;
while(fabs(item)>=1e-6){
sum+=item;
t++;
item/=t;
}
e = sum +1;
printf("e = %lf\n", e);
}
输出
e = 2.718282
#include
int main()
{
int n;
scanf("%d", &n);
int i;
int x;
double sum = 1.0;
int pro;
for(i = 1; i <= n; i++){
for(x = 1, pro = 1; x <= i; x++){
pro *= x;
}
sum += 1.0/pro;
}
printf("%f", sum);
return 0;
}
#include "stdio.h"
int main(int argc,char *argv[]){
int i,n;
double e,t;
printf("Input n(int 0<=n<=1000)...\nn=");
if(scanf("%d",&n)!=1 || n<0 || n>1000){
printf("Input error, exit...\n");
return 0;
}
for(e=t=1.0,i=1;i<=n;e+=t/=i++);
printf("e≈%.8f\n",e);
return 0;
}