(Approximate π) π can be computed using the following formula:
Ï€ = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...)
Write a program that displays the result of 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11) and 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13).
Use 1.0 instead of 1 in your program.
Answer
/**
Program - 07
Write a program that displays the result of 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11) and 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13).
Use 1.0 instead of 1 in your program.
**/
public class HelloWorld
{
public static void main(String args[])
{
System.out.println(4 * (1.0 - (1 / 3) + (1 / 5) -
(1 / 7) + (1 / 9) - (1 / 11)));
System.out.println(4 * (1.0 - (1 / 3) + (1 / 5) - (1 / 7) +
(1 / 9) - (1 / 11) + (1 / 13)));
}
}
OUTPUT
4.0
4.0
If you have any doubts or questions, please let me know.