I take my first Java programming class and this is my first class. I am so confused about how to approach him. Any help or correction would be appreciated.
You can approximate the value of the PI constant using the following series:
PI = 4 ( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + ( (-1)^(i+1) )/ (2i - 1) )
Ask the user for the value of i (in other words, how many terms in this series to use) to calculate PI. For example, if the user enters 10000, sum the first 10000 elements of the series and then show the result.
In addition to displaying the final result (your final PI approximation), I want you to display as your intermediate calculates at each power 10. Thus, 10, 100, 1000, 10000, etc. are mapped onto the PI approximation shielding with so many elements to be summed.
This is what I have done so far.
import java.util.Scanner;
public class CalculatePI {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Enter number of terms");
double i = input.nextDouble();
double sum = 0;
for(i=0; i<10000; i++){
if(i%2 == 0)
sum += -1 / ( 2 * i - 1);
else
sum += 1 / (2 * i - 1);
}
System.out.println(sum);
}
}
source
share