Advertisement
Guest User

Untitled

a guest
Nov 6th, 2021
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. #define EPSILON 0.0001
  6.  
  7. double factorial(double x)
  8. {
  9.     if (x <= 1) {
  10.         return 1;
  11.     } else {
  12.         return x*factorial(x-1);
  13.     }
  14. }
  15.  
  16. int main()
  17. {
  18.     int x;
  19.     printf("Enter x: ");
  20.     scanf("%i", &x);
  21.  
  22.     double answer = 0;
  23.     int sign = 1;      // For alternating signs
  24.     for (int n = 0 ;; n += 2) {
  25.         double new_term = sign * (pow(x, n) / factorial(n));
  26.         answer += new_term;
  27.         sign *= -1;
  28.        
  29.         if (abs(new_term) < EPSILON)
  30.             break;
  31.     }
  32.     printf("Answer: %f\n", answer);
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement