Leibniz's series pi/4 = 1 - 1/3 + 1/5 - 1/7 + ....

Write a program Q3.cpp to compute π by using the following formula.
pi/4 = 1 − 1/3 +1/5 − 1/7 +1/9 + ⋯ + (−1)^n/2n + 1
Q3.cpp should meet the following requirements:
a. Write function pi() to compute π
b. Use for loop to compute and output results of pi(1), pi(10), pi(100), pi(1000), pi(10000),
pi(100000), pi(1000000), pi(10000000), pi(100000000).


this is my code:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
float x,s;
float pi(float x);
int main(){

for(x=1;x<=10;x++)
{
s+=pi(x);

cout <<setprecision(15)<<"pi(10):"<< s <<endl;
} return 0;

}
float pi(float x)
{
return 4*(pow(-1,x-1)/(2*x-1));
}
Last edited on
Topic archived. No new replies allowed.