Advertisement
xotohop

cp16

Oct 31st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int n, i, sum, task, x, y, fact;
  5. float previous, current, sum_t = 0, t;
  6.  
  7. int sum3(int x, int y)
  8. {
  9.     sum = 0;
  10.     for (i = x; i <= y; i++)
  11.     {
  12.         if (i % 2 == 0)
  13.             sum += pow(i, 3);
  14.     }
  15.     return sum;
  16. }
  17.  
  18. int factorial(int n)
  19. {
  20.     fact = 1;
  21.     for (i = 1; i <= n; i++)
  22.         fact *= i;
  23.         if (fact == n)
  24.         {
  25.             printf("YES - %d!\n", i);
  26.             return 0;
  27.         }
  28.     return 0;
  29. }
  30.  
  31. double last(float x, int n)
  32. {  
  33.     return powf(-1, n)*(n - 1)*powf(x, n)/(n + 1);
  34. }
  35.  
  36. int main(void)
  37. {
  38.     printf("Choose the task: 1, 2 or 3:\n");
  39.     scanf("%d", &task);
  40.     if (task == 1)
  41.     {
  42.         printf("Enter the range [x,y]:\n");
  43.         scanf("%d%d", &x, &y);
  44.         printf("%d", sum3(x, y));
  45.     }
  46.     else if (task == 2)
  47.     {
  48.         printf("Enter the number N:\n");
  49.         scanf("%d", &n);
  50.         factorial(n);
  51.     }
  52.     else if (task == 3)
  53.     {
  54.         float x;
  55.         printf("Enter the number x (|x| < 1):\n");
  56.         scanf("%f", &x);
  57.         t = 1;
  58.         current = last(x, t);
  59.         sum_t += current;
  60.         //printf("%f %f\n", current, sum_t);
  61.         t++;
  62.         do
  63.         {
  64.             previous = current;
  65.             current = last(x, t);
  66.             sum_t += current;
  67.             //printf("%f %f %f\n", previous, current, sum_t);
  68.             t++;
  69.         } while (fabs(current - previous) > pow(10, -4));
  70.         printf("%f\n", sum_t);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement