NIKOLAY_TETUS

l2, ex7

Oct 10th, 2021
729
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdbool.h>
  4.  
  5. long fact(int n)
  6. {
  7.     if (n == 0)
  8.         return 1;
  9.     else
  10.         return(n * fact(n - 1));
  11. }
  12.  
  13. int main()
  14. {
  15.     long double epsilon = 1e-5;
  16.  
  17.     long double x;
  18.     printf("x> ");
  19.     scanf("%Lf", &x);
  20.  
  21.     int it = 2;
  22.     long double S = 1;
  23.  
  24.     long double prevNumber;
  25.     long double diffValue;
  26.  
  27.     short isNegative = true;
  28.  
  29.     do
  30.     {
  31.         prevNumber = S;
  32.         diffValue = pow(x, it) / (long double)fact(it + 1);
  33.  
  34.         if (isNegative)
  35.             S -= diffValue;
  36.         else
  37.             S += diffValue;
  38.  
  39.         it += 2;
  40.         isNegative = !isNegative;
  41.     }while(diffValue > epsilon);
  42.  
  43.     printf("S: %Lf", S);
  44.  
  45.     return 0;
  46. }
RAW Paste Data