//
根据公式计算e
//
// e = sum(i!); i= 0->n;
//
#include < stdio.h >
int Factorial( int n)
{
if (n <= 1 )
{
return 1 ;
}
return n * Factorial(n - 1 );
}
double Calculate_e( int n)
{
double sum = 0 ;
for ( int i = 0 ; i <= n; i ++ )
{
sum += 1 / ( double )Factorial(i);
}
return sum;
}
int main()
{
printf( " n e\n------------------------\n " );
for ( int i = 0 ; i < 10 ; i ++ )
{
if (i <= 1 )
{
printf( " %d %.0f\n " , i, Calculate_e(i));
}
else if (i == 2 )
{
printf( " %d %.1f\n " , i, Calculate_e(i));
}
else
{
printf( " %d %f\n " , i, Calculate_e(i));
}
}
return 0 ;
}
//
// e = sum(i!); i= 0->n;
//
#include < stdio.h >
int Factorial( int n)
{
if (n <= 1 )
{
return 1 ;
}
return n * Factorial(n - 1 );
}
double Calculate_e( int n)
{
double sum = 0 ;
for ( int i = 0 ; i <= n; i ++ )
{
sum += 1 / ( double )Factorial(i);
}
return sum;
}
int main()
{
printf( " n e\n------------------------\n " );
for ( int i = 0 ; i < 10 ; i ++ )
{
if (i <= 1 )
{
printf( " %d %.0f\n " , i, Calculate_e(i));
}
else if (i == 2 )
{
printf( " %d %.1f\n " , i, Calculate_e(i));
}
else
{
printf( " %d %f\n " , i, Calculate_e(i));
}
}
return 0 ;
}