Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pi, a mathematical constant commonly approximated as 3.14159, is the ratio of a

ID: 3791513 • Letter: P

Question

Pi, a mathematical constant commonly approximated as 3.14159, is the ratio of a circle's circumference to its diameter. It is usually represented by the Greek letter pi. Write a C++ program that approximates the value of pi using the Gregory-Leibniz formula: Product = 4 - 4/3 + 4/5 - 4/7 + 4/9. Since this formula is an infinite series, you will need to prompt the user for the number of iterations (summation terms) they want to use in the calculation. For example, if the user indicates "4" iterations, the result would be pi = 4 - 4/3 + 4/5 - 4/7 or approximately 2.89524. If the user indicates "10" iterations, the result would be pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 + 4/17 - 4/19 or approximately 3.04184. If the user indicated "10000" iterations, the result would be approximately 3.14149. Thus, as the number of summation terms increases, the result converges on true value of PI. Rather than putting all your code in main(), use functions to perform the calculation. Embed your program in a loop so that the calculation can be repeated multiple times.

Explanation / Answer

#include <iostream>

#include <iomanip>

#include <string>

#include <math.h>

using namespace std;

double Calculate(int);

int main()

{

int iteration;

   

        cout << "Please enter the number of iterations you would like to calculate : ";

            cin >> iteration;

                                                if (cin<=0)

            cout << "Invalid input.. Try Again . . .";

               else

Calculate(iteration);

    return 0;

}//main

double Calculate(int iteration)

{

double pi=0;

    for ( int i = 0; i < iteration ; i++ )

    {

       pi =pi+ (pow(-1,i)*4/(2*i+1));

       i=i++;

    }

cout << pi;

}