Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4.  
  5. float factorial(float x);
  6. float func(float fact_value);
  7. int is_equal(float val1, float val2, float eps);
  8.  
  9. int main()
  10. {
  11. float eps;
  12. float sum=0, i=1;
  13. printf("Enter epsilon: ");
  14. scanf("%f", &eps);
  15. while (is_equal(func(i), func(i-1), eps) != 1)
  16. {
  17. sum += func(i);
  18. // printf("%f is an element and %f is sum\n", func(i), sum);
  19. i++;
  20. }
  21. printf("Total sum is %f ", sum);
  22. getch();
  23. return 0;
  24. }
  25.  
  26. float factorial(float x)
  27. {
  28. float fact_value;
  29. for (int i = 1; i <= x; fact_value *= i++);
  30. if (x == 0)
  31. fact_value = 1;
  32. return fact_value;
  33. }
  34.  
  35. float func(float fact_value)
  36. {
  37. float func_value;
  38. return func_value = 1 / fact_value;
  39. }
  40.  
  41. int is_equal(float val1, float val2, float eps)
  42. {
  43. int result;
  44. result = fabs(val1 - val2) < eps;
  45. return result;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement