/* Name: Pi Calculator Beta, via integration of y=1/((a^2)*(x^2)). Author: Shamashis Sengupta. Description: This program approximates pi. Date: Monday, June 16, 2003. Copyright: This program is distributed under the GNU General Public License. Details: If y=1/((a^2)*(x^2)), then integral (y)dx=arctan(x/a)+c, where 'a' is a constant and 'c' is the integration constant. When the y vs. x curve is drawn, the area under the curve from x=0 to x=a is [arctan(a/a)-arctan(0/a)]=pi/4. In this program, the area under the above mentioned curve from x=0 to x=a is calculated by dividing the area into a number of thin strips. The width of each strip along the x-axis is unity. The value of 'a' should be input by the user. The sum of the area of all the strips is multiplied by 4 to calculate the approximate value of pi. */ #include int main() { double keeptrack=1.0,a,pi,x,dx,y,sum=0.0,split; printf("Enter the upper limit 'a'.\n"); scanf("%lf",&a); dx=1.0; split=a/100.0; for(x=dx;x<=a-dx;x=x+dx) { y=1/((a*a)+(x*x)); sum=sum+(y*dx); if(x>=keeptrack*split) { printf("\n%.0lf of 100 completed.",keeptrack); keeptrack++; } } sum=sum+((((1.0/(a*a))+(1/(2*a*a)))/2.0)*dx); pi=4.0*sum*a; printf("\nPi=%.15lf",pi); return(0); }