I need help writing a simple and easy to understand program
I need help writing a simple and easy to understand program using Java. Here is the exercise I need help with:
You can approximate pi by using the following series:
pi = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + . . . + (-1)i+1/ 2i - 1)
Write a program that displays the pi value for i = 10000, 20000, . . ., and 100000.
Solution
public class piValue {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double pi=0;
int i;
for(i=1;i<=10000;i++)
{
pi += Math.pow(-1, i) * (double)1/(2*i-1);
}
pi*=4;
System.out.println(\"Value of Pi with i =\"+(i-1)+\" is =\"+pi);
}
}
o/p
Value of Pi with i =10000 is =-3.1414926535900345
Put the value of i at which you want to calculate Pi
