Advertisement
Alx09

Untitled

Jun 22nd, 2020
1,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. #define pi 3.14159265359
  7.  
  8. double AproxPi(unsigned n){
  9.     double piAprox = 0, r;
  10.  
  11.     srand((time(NULL)));
  12.     while(n){
  13.         r = ((double) rand() / (RAND_MAX));
  14.         if(piAprox + r <= pi)
  15.             piAprox += r;
  16.         n--;
  17.     }
  18. return piAprox;
  19. }
  20.  
  21.  
  22. int main ()
  23. {
  24.     unsigned n;
  25.     cout <<"Numarul de numere generate: "; cin >> n;
  26.     cout <<"Rezultatul aproximari lui pi cu cele " << n << " generate este "<<AproxPi(n);
  27.  
  28. return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement