-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi.cpp
More file actions
42 lines (32 loc) · 743 Bytes
/
Copy pathpi.cpp
File metadata and controls
42 lines (32 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//pi.cpp
//Mason Corey
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int n;
double pi;
int i;
int num_terms;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
while(n!=-1) {
cout << "Enter the value of the parameter 'n' in the Leibniz formula (or -1 to quit):" << endl;
cin >> n;
pi = 0;
for(i = 0; i<=n; i++) {
pi += ((pow(-1,i))/(2*i+1));
}
pi *= 4;
num_terms = n + 1;
if(n!=-1) {
if(n==0) {
cout << "The approximate value of pi using 1 term is: " << pi << endl;
}
else {
cout << "The approximate value of pi using " << num_terms << " terms is: " << pi << endl;
}
}
}
}