Lab4Modified
unknown
plain_text
3 years ago
865 B
10
Indexable
#include<stdio.h> #include<math.h> #include<stdlib.h> #define actualPi 22/7.0 int main() { const double r=9; int i, m, n,itr=0; double x,y,z,pi; int arr[1]={0,1}; printf("Enter the number of iterations you want= "); scanf("%d", &n); int count= 0; for(i = 0; i < n; ++i) { x = ((double)rand() / (RAND_MAX)) *r; y = ((double)rand() / (RAND_MAX))*r; z = x * x + y * y; double distance= sqrt(z); if( distance <=r ) { itr++; count++; printf("%d %lf %lf %lf %d",itr,x,y,distance,count); printf("\n"); } else{ itr++; printf("%d %lf %lf %lf %d",itr,x,y,distance,count); printf("\n"); } } pi= (double) (4*count)/itr; printf("\nValue of pi :%lf",pi); double error= (actualPi-pi)/actualPi; if (error<0){ error*=-1; } printf("\nError Percentage is: %lf ",error*100); return(0); }
Editor is loading...