MUST BE DONE IN JAVA The value of p can be computed accordin
MUST BE DONE IN JAVA
The value of p can be computed according to the following formula:
Write an algorithm and program to compute p. Because the formula is an infinite series and an algorithm must stop after a finite number of steps, you should stop when you have the result determined to six significant digits.
1-9 1-7 13 -4Solution
public class PI
{
public static void main (String[] args)
{
double sum=0;
double pi,num,den;
num=den=1;
for(int i =1;i<37;i++)
{
pi =num/den;
sum = sum +pi;
//System.out.print(pi+\" \\t \");
num = -num; //numerator is multiplied by -1
den = den+2; //denominator is incremented to 2
}
//System.out.println(\"pi= \"+ sum*4);
System.out.printf(\"pi = %.6f\", + sum*4); //output formatting
}
}
output:
