Respuesta :
Answer:
- #include <iostream>
- using namespace std;
- int main()
- {
- int t = 2;
- int divisor = 3;
- float l_series=1;
- bool flag = true;
- while(t <= 10000){
- if(flag){
- l_series = l_series - (1/(float)(divisor));
- flag = false;
- }else{
- l_series = l_series + (1/(float)(divisor));
- flag = true;
- }
- t++;
- divisor+=2;
- }
- cout<<"PI value: "<< l_series * 4;
- }
Explanation:
Firstly, we declare several variables t, divisor, l_series and flag. The variable t denotes the current term. We start the term with 2. We can write the Leibniz formula in form of this expression
l_series - (1/(float)(divisor))
or
l_series + (1/(float)(divisor))
We put the expression in a while loop and put a flag in an if statement. If the flag is true, run the expression l_series - (1/(float)(divisor)) and then set the flag to false (Line 12 -14).
If the flag is false run the expression l_series = l_series + (1/(float)(divisor)) and then set the flag to true.
Next increment the term by one and divisor by two before proceeding to the next term of calculation.
At the end, print the approximated PI value by multiplying l_series by 4. If we count for 10000 terms, we shall get the PI value 3.1415