0 Members and 1 Guest are viewing this topic.
/* * @author sathe * 10 Feb 2013 * * Computes pi using the Leibniz formula found at * http://en.wikipedia.org/wiki/Leibniz_formula_for_pi * */#include <iostream>#include <math.h>#include <iomanip>using namespace std;double myPrompt(void);double compute(double);int main(){ cout << "Computing Pi Series Summation by ML Formula" << endl; cout << "===========================================" << endl; double kLimit = myPrompt(); double approx = compute(kLimit); cout << "Approximation of pi is " << fixed << setprecision(15) << approx << endl; return 0;}double myPrompt(void){ double kLimit = 0; do { cout << "Enter a maximum value of k in truncated series (non-negative): "; cin >> kLimit; }while(kLimit < 0); return kLimit;}double compute(double kLimit){ double approx = 0; for(double i = 0; i <= kLimit; i++) { approx += 4 * (pow(-1, i)/((2 * i) + 1)); } return approx;}