While solving the question(It is in the code as comment.) The if statement (which displays the result) is not working. Please help me to figure out the error. It gets compiled and runs but output is not displaying.
/*
* (Calculating the Value of π ) Calculate the value of π from the infinite series
4 4 4 4 4
π = 4 – --- + --- – --- + --- – ------ + ...
3 5 7 9 11
Print a table that shows the value of π approximated by computing the first 200,000 terms of this
series. How many terms do you have to use before you first get a value that begins with 3.14159?
*/
public class ValueOfPi
{
public static void main(String[] args)
{
int i,count=0;
double pi=0.0,n=1.0,PI=3.14159;
for(i=0;i<200000;i++)
{
count++;
if(count%2!=0)
{
pi=pi+(4/n);
}
else if(count%2==0)
{
pi=pi-(4/n);
}
n=n+2;
if(pi==PI)
{
System.out.printf("terms=%d PI=%f\n",count,pi);
break;
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire