#include
int main(void)
{
int n,i,t;
double sum=0;
printf("输入n:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
t*=i;
sum+=1.0/t; /*由于sum是浮点数 所以要用这个形势
}
printf("sum=%lf",sum);
return 0;
}
这种写法因该是标准的!仅供参考
#include "stdio.h"
double fac(int a)
{
double n;
if(a==0)n=1;
else
if(a==1)n=1;
else
n=fac(a-1)*a;
return(n);
}
main()
{
int i,n;
double s=0,a=1.0,t;
printf("please input a number:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
t=fac(i);
s+=a/t;
}
printf("s=1+1/1!+1/2!+1/3!+....+1/%d!+.....=%18.17f",n,s);
}
/*
运行结果:
please input a number:10
s=1+1/1!+1/2!+1/3!+....+1/10!+.....=2.71828180114638451
*/
#include
void main()
{
int n,i,t;
double sum=0;
printf("请输入n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
t*=i;
sum+=1/t;
}
printf("sum=%lf",sum);
}