// estimating the PI using Monte Carlo Simulation in C
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define SEED time(NULL)
int main(){
srand( SEED );
int i, count, n;
double x,y,z,p1;
printf("n = ");
scanf("%d", &n);
count = 0;
for(i=0; i<n; i++){
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
z=x*x+y*y;
if(z<=1){
count++;
}
}
p1=(double)count/n*4;
printf("Approximate PI = %g", p1);
return 0;
}
Ly8gZXN0aW1hdGluZyB0aGUgUEkgdXNpbmcgTW9udGUgQ2FybG8gU2ltdWxhdGlvbiBpbiBDCiNpbmNsdWRlPHN0ZGlvLmg+CiNpbmNsdWRlPG1hdGguaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPHRpbWUuaD4KI2RlZmluZSBTRUVEIHRpbWUoTlVMTCkKaW50IG1haW4oKXsKICAgIHNyYW5kKCBTRUVEICk7CiAgICBpbnQgaSwgY291bnQsIG47CiAgICBkb3VibGUgeCx5LHoscDE7CiAgICBwcmludGYoIm4gPSAiKTsKICAgIHNjYW5mKCIlZCIsICZuKTsKICAgIGNvdW50ID0gMDsKICAgIGZvcihpPTA7IGk8bjsgaSsrKXsKICAgICAgICB4PShkb3VibGUpcmFuZCgpL1JBTkRfTUFYOwogICAgICAgIHk9KGRvdWJsZSlyYW5kKCkvUkFORF9NQVg7CiAgICAgICAgej14KngreSp5OwogICAgICAgIGlmKHo8PTEpewogICAgICAgICAgICBjb3VudCsrOwogICAgICAgIH0KICAgIH0KICAgIHAxPShkb3VibGUpY291bnQvbio0OwogICAgcHJpbnRmKCJBcHByb3hpbWF0ZSBQSSA9ICVnIiwgcDEpOwogICAgcmV0dXJuIDA7Cn0=