Guest User

Untitled

a guest
Feb 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. //a program that approximate e by computing values
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6. //initialise variables for use
  7. float num, result=2;
  8.  
  9. //scan for the number
  10. scanf("%f", &num);
  11.  
  12. //factorial method
  13. float factorial(int n){
  14. if(n <= 1)
  15. return 1;
  16. else
  17. return n*factorial( n-1 );
  18. }
  19.  
  20. //while loop that checks if the factorial is less than the num given
  21. int i = 2;
  22. while((1/factorial(i)) < num){
  23. result = result + (1/factorial(i));
  24. i++;
  25. }
  26.  
  27. //return the result with 5 decimals
  28. printf("%.5f\n", result);
  29.  
  30. //return result
  31. return 0;
  32. }
Add Comment
Please, Sign In to add comment