fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main() {
  5. double pi = 0.0, term;
  6. int n = 0;
  7. do {
  8. term = pow(-1, n) / (2 * n + 1);
  9. pi += term;
  10. n++;
  11. } while (fabs(term) >= 1e-6);
  12. pi *= 4;
  13. printf("π 的近似值为:%.8f\n", pi);
  14. return 0;
  15. }
Success #stdin #stdout 0.02s 5320KB
stdin
Standard input is empty
stdout
π 的近似值为:3.14159465