求和n!,n从1取到20
解:程序:
#include <stdio.h>
int main()
{
float s=0,t=1;
int n;
for (n=1;n<=20;n++)
{
t=t*n; //求n!
s=s+t; //将各项累加
}
printf("%f\n",s);
return 0;
}
输出结果:
2561327455189073900.000000
Press any key to continue
求和1/n!,n从1取到20
解:程序:
#include <stdio.h>
void main()
{
double s=0,t=1;
double n;
for (n=1;n<=20;n++)
{
t=t*(1/n); //求1/(n!)
s=s+t; //将各项累加
}
printf("%lf\n",s);
}
运行结果:
1.718282
Press any key to continue