Name: Jim Profit 2013-02-13 0:11
Alright, for the life of me, I can't figure out what is wrong with my code. I'm trying to use this formula to calculate pi in c++. I've tried it with a while loop too, but it always gives me pi = 1. Can anyone help me out?
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
char reply;
double pi=0;
long n;
long i;
cout << "Enter the value of n: ";
cin >> n;
// Set the state of the output stream to format using 2 places of decimals.
cout << setprecision(4);
cout << fixed << setprecision(2);// Set the output stream to display floating point numbers in fixed point notation
for (i = 0; i <= n; i++) {
if (i%2 == 0)
pi = (pi+(1 / (2 * i + 1)));
else
pi = pi-(1 / (2 * i + 1));
cout << pi << endl;
}
pi = pi * 4;
cout << pi;
// "Pause" so the user can see the results
cout << "\nEnter q to quit... ";
cin >> reply;
return 0;
}
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
char reply;
double pi=0;
long n;
long i;
cout << "Enter the value of n: ";
cin >> n;
// Set the state of the output stream to format using 2 places of decimals.
cout << setprecision(4);
cout << fixed << setprecision(2);// Set the output stream to display floating point numbers in fixed point notation
for (i = 0; i <= n; i++) {
if (i%2 == 0)
pi = (pi+(1 / (2 * i + 1)));
else
pi = pi-(1 / (2 * i + 1));
cout << pi << endl;
}
pi = pi * 4;
cout << pi;
// "Pause" so the user can see the results
cout << "\nEnter q to quit... ";
cin >> reply;
return 0;
}