-
-
Save johnciacia/1090849 to your computer and use it in GitHub Desktop.
Calculate the value of e as the sum of the infinite series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/** | |
* This code will calculate e | |
* as the sum of the infinite series | |
* 1/1! + 1/2! + 1/3! + ... + 1/n! | |
* g++ e.c | |
* ./a.out | |
*/ | |
int factorial(unsigned int i); | |
int main() { | |
double e = 1.0; | |
for (int n = 10; n > 0; n--) | |
e += 1.0/factorial(n); | |
printf ("e = %lg\n", e); | |
return 0; | |
} | |
int factorial(unsigned int i) { | |
if(i == 0 || i == 1) | |
return 1.0; | |
return i * factorial(i-1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment