# Answer to Question #26316 in C++ for kassem

Question #26316

I want to write a program to calculate Pi:Pi=4-4/3+4/5-4/7+4/9-4/11+...(the user inputs the number of terms in the series)

Expert's answer

#include <iostream>

using namespace std;

double calc_PI(int termCount)

{

double result = 0.0;

double divider = 1;

char sign = 1;

while (termCount--)

{

result += (4 * sign / divider);

divider += 2;

sign = -sign;

}

return result;

}

int main()

{

int termCount;

cout << "Enter the number of series: ";

cin >> termCount;

cout << "PI = " << calc_PI(termCount);

return 0;

}

using namespace std;

double calc_PI(int termCount)

{

double result = 0.0;

double divider = 1;

char sign = 1;

while (termCount--)

{

result += (4 * sign / divider);

divider += 2;

sign = -sign;

}

return result;

}

int main()

{

int termCount;

cout << "Enter the number of series: ";

cin >> termCount;

cout << "PI = " << calc_PI(termCount);

return 0;

}

## Comments

## Leave a comment