This program is not related with Numerical Method but if you want to have Monte Carlo method to find Value of Pi with simulation then this ...
This program is not related with Numerical Method but if you want to have Monte Carlo method to find Value of Pi with simulation then this is it.
Program
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
#define seed 10000
void main()
{
int gd=DETECT,gm;
int niter=0,i,count=0;
double x,y,z,pi;
initgraph(&gd,&gm,"..\\bgi");
printf("Enter the number of iteration used to estimate pi");
scanf("%d",&niter);
line(100,200,300,200);
line(100,20,100,200);
rectangle(100,50,250,200);
arc(100,200,0,90,150);
srand(seed);
count=0;
for(i=0;i<niter;i++)
{
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
z=x*x+y*y;
if(z<=1)
{
putpixel(100+150*x,200-150*y,YELLOW);
count++;
}
else
{
putpixel(100+150*x,200-150*y,BLUE);
}
delay(50);
}
pi=4*count/niter;
printf("No of trial = %d , estimate of pi is %g",niter,pi);
getch();
}
Output
Program
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
#define seed 10000
void main()
{
int gd=DETECT,gm;
int niter=0,i,count=0;
double x,y,z,pi;
initgraph(&gd,&gm,"..\\bgi");
printf("Enter the number of iteration used to estimate pi");
scanf("%d",&niter);
line(100,200,300,200);
line(100,20,100,200);
rectangle(100,50,250,200);
arc(100,200,0,90,150);
srand(seed);
count=0;
for(i=0;i<niter;i++)
{
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
z=x*x+y*y;
if(z<=1)
{
putpixel(100+150*x,200-150*y,YELLOW);
count++;
}
else
{
putpixel(100+150*x,200-150*y,BLUE);
}
delay(50);
}
pi=4*count/niter;
printf("No of trial = %d , estimate of pi is %g",niter,pi);
getch();
}
Output
COMMENTS