Skip to content

Commit 1fc84c8

Browse files
authored
Merge pull request kelvins#317 from pluto-tofu/leibniz_pi
added leibniz formula for calculating pi in c++
2 parents 7b21a40 + f42682c commit 1fc84c8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3732,8 +3732,8 @@ In order to achieve greater coverage and encourage more people to contribute to
37323732
</a>
37333733
</td>
37343734
<td> <!-- C++ -->
3735-
<a href="./CONTRIBUTING.md">
3736-
<img align="center" height="25" src="./logos/github.svg" />
3735+
<a href="src/cpp/LeibnizFormulaForPi.cpp">
3736+
<img align="center" height="25" src="./logos/cplusplus.svg" />
37373737
</a>
37383738
</td>
37393739
<td> <!-- Java -->

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)