First thing I see is you attempting to return a value from your void main method.

don't return pi; from your main method. Print it.

CopySystem.out.println(pi);

Secondly, when people write a for loop, they're commonly iterating over i, which is probably the same i that your professor referred to in the formula

Copyfor(double i=0; i<SomeNumber; i++)
{
    sum += ((-1)^(i+1)) / (2 * i - 1) );
}

now, that won't work correctly as is, you still have to handle the ^, which java doesn't use natively. luckily for you, -1^(i+1) is an alternating number, so you can just use an if statement

Copyfor(double i=0; i<SomeNumber; i++)
{
    if(i%2 == 0) // if the remainder of `i/2` is 0
        sum += -1 / ( 2 * i - 1);
    else
        sum += 1 / (2 * i - 1);
}
Answer from Sam I am says Reinstate Monica on Stack Overflow
🌐
Instructables
instructables.com › circuits › computers
Calculate Pi in Java! : 12 Steps - Instructables
April 29, 2024 - Calculate Pi in Java!: Pi, a well known number equaling 3.1415926... But how can we actually calculate Pi to such incredible accuracy (105 trillion digits in 2024)? Are they actually measuring pieces of string around circles? No, of course not. They use clever algorithms …
🌐
Medium
connect2grp.medium.com › java-calculate-value-of-pi-using-math-formulas-part-1-66362755b998
Java : Calculate Value of PI using Math Formulas-Part-1 | by A ...
January 10, 2026 - Some famous examples of formulas for calculating the value of π include the Leibniz formula, the Monte Carlo method, the Bailey–Borwein–Plouffe formula, and the Srinivasa Ramanujan formula , Newotn Formula etc. In Modern Mathematics, π is represented by the symbol “π” and is often used in formulas to represent the value of the ratio of the circumference of a circle to its diameter. For example, the… ... A humble place to learn Java and Programming better.
🌐
Study.com
study.com › courses › business courses › business 104: information systems and computer applications
How to Use Pi Constant in Java - Lesson | Study.com
January 15, 2018 - This shows right away that your program will be using math for its calculations. The import code looks like this: ... Even though pi goes on seemingly forever, Java stores the value of pi as a constant, or 3.141592653589793.
🌐
Baeldung
baeldung.com › home › algorithms › java program to estimate pi
Java Program to Estimate Pi | Baeldung
January 8, 2024 - Learn how to estimate the value of pi using the Monte Carlo algorithm in Java.
🌐
GitHub
github.com › Nefari0uss › calculate-pi › blob › master › Pi.java
calculate-pi/Pi.java at master · Nefari0uss/calculate-pi
return 4 * pi; } · · private static int getInput() { int n = 0; Scanner input = new Scanner(System.in); · System.out.println("How many calculations should be run for the approximation?"); try { n = Integer.parseInt(input.nextLine()); } /** Handle input greater than Java's MAX_INT.
Author   Nefari0uss
🌐
Coderanch
coderanch.com › t › 662590 › java › Pi-Calculation-program
Pi Calculation program (Beginning Java forum at Coderanch)
This program asks the user to input a number on to how many times there has to be a calculation to PI. So that below pi and not OK.
🌐
YouTube
youtube.com › watch
Shortest code to calculate Pi by Java - YouTube
Calculating the Number PI Through Infinite Sequence pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13
Published   December 9, 2016
Find elsewhere
🌐
YouTube
youtube.com › watch
69 Java Series Calculate Pi Code | - YouTube
Launch Your First Android app with our TOP course at 82% OFF (24 hrs ONLY) HERE https://goo.gl/7veBXc"Learn How To Design + Code A Complete App From Scratch ...
Published   February 17, 2013
🌐
Oracle
docs.oracle.com › javase › tutorial › rmi › examples › client › Pi.java
client.Pi
*/ public BigDecimal execute() { return computePi(digits); } /** * Compute the value of pi to the specified number of * digits after the decimal point. The value is * computed using Machin's formula: * * pi/4 = 4*arctan(1/5) - arctan(1/239) * * and a power series expansion of arctan(x) to * ...
🌐
CodeGym
codegym.cc › java blog › java math › math.pi in java
Math.PI in Java
December 5, 2024 - Math.PI is a static final double constant in Java, equivalent to in π Mathematics. Provided by java.lang.Math class, Math.PI constant is used to carry out multiple mathematical and scientific calculations like finding the area & circumference of a circle or the surface area and volume of a sphere.
🌐
Cronodon
cronodon.com › Programming › Pi.html
Estimating pi
= 3 628 800 (0! is set equal to 1 by definition). These numbers rapidly get very big! The highest my pocket calculator can go is 69! = 1.711224524 E+98. For the 20th term, k = 20, we already need to compute 80! It would be straightforward to multiply Java (or C#) integers (int) together to compute n!
🌐
Kodejava
kodejava.org › how-do-i-get-the-pi-value
How do I get the PI value? - Learn Java by Examples
We use the Math.PI static field to get the value of PI. package org.kodejava.math; public class GetPiExample { public static void main(String[] args) { // The PI value represented by Math.PI System.out.println("PI = " + Math.PI); // Using the ...
🌐
Delft Stack
delftstack.com › home › howto › java › pi in java
Pi Constant in Java | Delft Stack
March 11, 2025 - One of the simplest and most effective ways to access the value of pi in Java is through the Math class. Java’s Math.PI provides a double value of pi, which is approximately 3.14159.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Pi in Java Example - Java Code Geeks
June 15, 2021 - Check out our detailed example about Pi in Java! Print out the math.pi java value and use it to calculate a circle's circumference and area.
Top answer
1 of 2
1

Sorry, but this is not a good idea. The formula that you saw essentially expresses that $$\sin x\approx x$$ when $x$ is small, and the smaller $x$ the more exact the approximation. It is valid for angles in radians.

When the angles are in degrees, this relation becomes

$$\sin°x\approx \frac{\pi x}{180}$$ where $\sin°$ denotes the sine of an angle in radians. So you hope to evaluate

$$\pi\approx180\frac{\sin°x}x.$$

If the function $\sin°$ is not available, you will have to emulate it with an explicit conversion, using

$$\sin°x=\sin\frac{\pi x}{180},$$ so that

$$\pi\approx180\frac{\sin\dfrac{\pi x}{180}}x.$$

So, not only this does not allow you to compute $\pi$ as it requires preliminary knowlegde of $\pi$, but it will do that in a very inefficient and inaccurate way, actually replacing $cx/x$ by $\sin cx/x$. You will spend much energy to go round in circles.

Even when a $\sin°$ function is available, this approach is wrong because the $\sin°$ will do the conversion from degrees to radians anyway (using a hard-coded value of $\pi$), and you will have to use an angle so small that $\sin x=x$ numerically, and there is no more point computing the sine.


A less "schizophrenic" approach is using

$$\pi=4\arctan1$$ (in radians).

2 of 2
0

I am assuming your sin(x) function takes radians, as stated in the OP. Start by assigning x = 3 and then iterate the statement x = x + sin(x) a few times (three iterations should work well). When you are done, x will contain a good approximation to $\pi$.

How does it work? We are using "fixed point iteration" to approximate a root of $x = x + \sin(x)$ near $x = 3$. Reference: Wikipedia, "Fixed-point iteration"

🌐
Java2Blog
java2blog.com › home › math › pi in java
PI in Java - Java2Blog
January 11, 2021 - So, Let’s see some examples to understand the use of PI in Java. Here, we created a constant our own and assigned a value to it. We declared it as final so, cannot be changed. Here, we are calculating the area and circumference of circle by using the created PI constant.