javacodex.com Java Examples
 Home FAQ Blog Code2HTML
Home > Math Examples > Leibniz Formula For PI

# Leibniz Formula For PI

Leibniz Formula for PI (aka Gregory Leibniz Series):
```   1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + 1/17 ... = pi/4
```

## Source: (LeibnizFormula.java)

```public class LeibnizFormula {
public static void main(String[] args) {

int count = 999999999;
double pi = 0;
double denominator = 1;

for (int x = 0; x < count; x++) {

if (x % 2 == 0) {
pi = pi + (1 / denominator);
} else {
pi = pi - (1 / denominator);
}
denominator = denominator + 2;
}
pi = pi * 4;
System.out.println(pi);
}
}
```

## Output:

```# java LeibnizFormula
3.1415926545880506
```

## Related Examples:

 Area Of A Circle Armstrong Numbers Bit Shift Calculate Volume and Surface Area of a Sphere Calculating Compound Interest Collatz Conjecture Converting Celsius to Fahrenheit Converting Fahrenheit to Celsius Decimal To Hex Converter Dot Product Euclidean Algorithm Exponents Factorial Recursion Example Fibonacci Numbers Fibonacci Recursion Example Find The Mean Value Of A Set Of Numbers Find The Median Value Of A Set Of Numbers Find The Mode Of A Set Of Numbers Find The Range Of A Set Of Numbers Floor and Ceiling Example Floyds Triangle Generate Random Numbers Within A Range Harmonic Number In Place Swap Infinity Examples Java Math Methods Keith Number Leibniz Formula For PI List Of Divisors Mortgage Calculator Pascal Triangle Prime Numbers Quadratic Formula Round A Number To N Decimal Places Sets Union Intersection Difference and Complement Sieve Of Eratosthenes Sum Of Squares Test If Number Is Odd Or Even Testing Subsets Without Using Collections Time It Takes For Light To Travel From The Sun To The Earth Tower Of Hanoi Triangle Numbers