Skip to content

Commit 7ecccce

Browse files
committed
added leibniz formula for calculating pi in c++
1 parent 7b21a40 commit 7ecccce

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/cpp/LeibnizFormulaForPi.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
float pi = 0.0;
5+
float denominator = 1.0;
6+
float operation = 1.0;
7+
8+
float pi_calculator(int terms)
9+
{
10+
for (int i = 0; i < terms; i++)
11+
{
12+
pi += operation * (4.0 / denominator);
13+
denominator += 2.0;
14+
operation *= -1.0;
15+
}
16+
return pi;
17+
}
18+
19+
int main()
20+
{
21+
float result = pi_calculator(100000);
22+
cout << result;
23+
}

0 commit comments

Comments
 (0)