fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double calculate_x(int n) {
  5. double sum = 0.0;
  6. for (int i = 0; i < n; i++) {
  7. double term = pow(-1, i) / (2 * i + 1);
  8. sum += term;
  9. }
  10. return 4 * sum;
  11. }
  12.  
  13. int main() {
  14. int n1 = 3;
  15. int n2 = 10;
  16.  
  17. double x_n1 = calculate_x(n1);
  18. double x_n2 = calculate_x(n2);
  19.  
  20. printf("x(%d) = %f\n", n1, x_n1);
  21. printf("x(%d) = %f\n", n2, x_n2);
  22.  
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
x(3) = 3.466667
x(10) = 3.041840