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 OverflowFirst 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);
}
sometimes the answer is a simple line of code. Also you are reassigning i to 0 so Im assuming you are using the user input in the wrong way.
Copyfor(int counter=1;counter<input;counter++){
sum += Math.pow(-1,counter + 1)/((2*counter) - 1);
}
for input that should can be any variable, hard coded or set by user input (such as 1000, 10000, 100000). This should work
The Math.PI is equal to 3.141592653589793, which isn't really all of pi of course. Is there any way to access like more of it? I'm making a simple program that asks the user for a number (number of the precision of an output of pi) and it feels a little lackluster since it only goes to 15 decimal places.
Is there any way around it? Either way, I'd love to understand why it's limited how it is
You're missing the multiplication operator. Also, you want to do 4/3 in floating point, not integer math.
volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
^^ ^
Here is usage of Math.PI to find circumference of circle and Area
First we take Radius as a string in Message Box and convert it into integer
public class circle {
public static void main(String[] args) {
// TODO code application logic here
String rad;
float radius,area,circum;
rad = JOptionPane.showInputDialog("Enter the Radius of circle:");
radius = Integer.parseInt(rad);
area = (float) (Math.PI*radius*radius);
circum = (float) (2*Math.PI*radius);
JOptionPane.showMessageDialog(null, "Area: " + area,"AREA",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "circumference: " + circum, "Circumfernce",JOptionPane.INFORMATION_MESSAGE);
}
}
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).
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"