Assignment-3

by Erdenebayar Dambasuren

PART 1

The following program calculates the value of PI using the infinite series :
  4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...


class Pi_series {
    final static int Nmax=10;

    public static void main(String[] args){
	int k=1;
        double sum=1,pi;
	boolean sign=false; 

    	for(int n=1;n < Nmax;n++){
	    k=k+2;
	    if (sign) {
		sum=sum+1./k;
	    } else {
		sum=sum-1./k;
	    };
	    sign=!sign;      
    	};
	pi=sum*4.;
    	System.out.println(pi);
    	System.out.println(Math.PI);
    	System.out.println(pi-Math.PI);
    }
} 


Running this program at different values of Nmax we can comstruct the table
   Nmax    Pi
     1    2.66667
     2    3.46667
     3    2.89524
     4    3.33968
     5    2.97605
     6    3.28374
     7    3.01707
     8    3.25237
     9    3.04184
    10    3.23232
    11    3.0584
    12    3.2184
    13    3.07025
    14    3.20819
    15    3.07915
    16    3.20037
    17    3.08608
    18    3.19419
    19    3.09162
Continuing this process I've got 3.14 (plus minus 0.005) after 152 loops, 3.141 (plus minus 0.0005) after 916 loops, 3.1415 (plus minus 0.00005) after 7010 loops, 3.14159 (plus minus 0.000005) after 130658 loops.

PART 2

In this part of assignment I modified a program ShowData.java in following:

Here is the result:

Source code: ShowDataMy.java

Home assignments:

Assignment-1

Assignment-2

Assignment-4

Assignment-5

Assignment-6

See a project:

Project