fork download
  1. #include <stdio.h>
  2. double calculate_sum(int n) {
  3. double sum = 0.0;
  4. for (int k = 1; k <= n; k++) {
  5. sum += 1.0 / (k * (k + 1)); // セミコロンを付けて修正
  6. }
  7. return sum;
  8. }
  9.  
  10. int main(void) {
  11. int n1 = 3;
  12. int n2 = 10;
  13. double result_n1 = calculate_sum(n1);
  14. double result_n2 = calculate_sum(n2);
  15.  
  16. printf("x(%d) = %f\n", n1, result_n1);
  17. printf("x(%d) = %f\n", n2, result_n2);
  18.  
  19. return 0;
  20. }
Success #stdin #stdout 0.01s 5256KB
stdin
Standard input is empty
stdout
x(3) = 0.750000
x(10) = 0.909091