fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. double approx_pi(void);
  5.  
  6. int main(void) {
  7. printf("pi = %lf\n", approx_pi());
  8. return EXIT_SUCCESS;
  9. }
  10.  
  11. double approx_pi(void) {
  12. double x, y, pi;
  13. int count = 0;
  14. for (int i = 1; i <= 1000; i++) {
  15. x = rand() / (double)(RAND_MAX + 1);
  16. y = rand() / (double)(RAND_MAX + 1);
  17. if (x * x + y * y < 1) {
  18. count++;
  19. }
  20. }
  21. pi = 4 * count / (double)1000;
  22. return pi;
  23. }
Success #stdin #stdout 0s 5536KB
stdin
Standard input is empty
stdout
pi = 3.132000