I am a c++ program beginner. I am learning c++ on my own. One of the exercise is to calculate pie=4-4/3+4/5-4/7+4/9-4/11.... I have try many ways and I cannot get it to work. Can you please tell me what is wrong? Thank you.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float pie=0;
int count = 0;
for(int i=0,j=1;i<=1000;i++,j=-j)
{
if (i%2==0)
if
pie += (4/((i*2)+1));
else
pie-= (4/((i*2)+1));
// float t=2*i+1;
// pie=pie+4*(j/t);
cout<<count++<<setw(30)<<fixed<<setprecision(16)<<pie<<endl;
}
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float pie=0;
int count = 0;
for(int i=0,j=1;i<=1000;i++,j=-j)
{
if (i%2==0)
if
pie += (4/((i*2)+1));
else
pie-= (4/((i*2)+1));
// float t=2*i+1;
// pie=pie+4*(j/t);
cout<<count++<<setw(30)<<fixed<<setprecision(16)<<pie<<endl;
}
return 0;
}