计算并输出e
公式:e=1+ 1/1!+1/2!+...+1/n!输入:一个整数n。
输出:公式计算的结果e的值
程序代码:#include <stdio.h>
double foo( unsigned n )
{
double result = 1;
double tmp = 1;
for( unsigned i=0; i!=n; ++i )
{
tmp /= i+1;
result += tmp;
}
return result;
}
//#include <assert.h>
int main( void )
{
//assert( foo(0) == 1 );
//assert( foo(1) == 1 + 1/1 );
//assert( foo(2) == 1 + 1/1 + 1/2. );
//assert( foo(3) == 1 + 1/1 + 1/2. + 1/6. );
unsigned n;
scanf( "%u", &n );
printf( "%f\n", foo(n) );
}[此贴子已经被作者于2022-10-31 14:49编辑过]