example1b.c 484 B

1234567891011121314151617181920212223242526272829303132
  1. #include <stdio.h>
  2. #include <math.h>
  3. double calculate_sin(double x, int n) {
  4. double result = 0.0;
  5. double term = x;
  6. double sign = 1.0;
  7. int i;
  8. for (i = 1; i <= n; ++i) {
  9. result += sign * term;
  10. sign = -sign;
  11. term *= x * x / ((2 * i) * (2 * i + 1));
  12. }
  13. return result;
  14. }
  15. int main() {
  16. double x;
  17. int n;
  18. scanf("%lf %d", &x, &n);
  19. double sin_x = calculate_sin(x, n);
  20. printf("%.8lf\n", sin_x);
  21. return 0;
  22. }