#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#define LIMIT 100000000 //max value of n void main(){ double mypi,x,y ; int minus ; // 1 for pos., 2 for neg.
long int m,n;
time_t t;
n = 0; m = 0;
srand((unsigned) time(&t));
clrscr();
do{
n++; x = (rand()/(double)RAND_MAX); //pick a random X between 0..1
minus = 1+(rand()/(double)RAND_MAX);//pick a sign for X
switch (minus) {
case1 : x = x; case2 : x = -1*x; }y = rand()/(double)RAND_MAX;//pick a random Y between 0..1
minus = 1+rand()/(double)RAND_MAX;//pick a sign for Y
switch (minus) {
case1 : x = x; case2 : x = -1*x; }if (pow(x,2)+pow(y,2) < 1) m++; //the point is inside the unit circle mypi = 4.0*m/n; //calculate pi if (n % (LIMIT / 1000) == 0) {
gotoxy(1,1);
printf("X:%f ",x);
printf("Y:%f ",y);
printf("n:%ld ",n);
printf("m:%ld ",m);
printf(" - ");
printf("PI:%f ",mypi);
}
}
while (n != LIMIT); getch();

}

Recommended Answers

All 2 Replies

Is it showing a compiler error? post the errors
Is it showing unwanted output? post the output
Do you know how to properly format your code and wrap it it in code tags before you post? If yes then do so

Is it showing a compiler error? post the errors
Is it showing unwanted output? post the output
Do you know how to properly format your code and wrap it it in code tags before you post? If yes then do so

thank you very much for your reminding, I really have not wrap all of code before post. It disappear about 1 or 2 lines. Now I can solve it .
^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.