I made a program that calculates pie(i think it works) but the answer is always rounded i get like 4.0.
I also noticed that if i System.out.println(3/4) it doesnt output a decimal number.
Can anyone help me?
Thanks.
Edit: Btw the program should output around 3.1 something when you enter 10 as number of iterations. (around pie)
Code:import java.util.Scanner; public class pie { // pi * 4 = 1 - (1/3)+(1/5)-(1/7)+... public static void main(String[] args) { Scanner input = new Scanner(System.in); int iter; double pie; boolean minus = true; System.out.println("<<<< Pie Calculator >>>>"); System.out.print("\nEnter The Number of iterations to use to approximate pie: "); iter = input.nextInt(); pie = 1.00001; for (int i=3 ; i < iter ; i+= 2) { if (minus= true) { pie -= 1/i ; minus = false; } else { pie += 1/i; minus = true; } } pie *= 4; System.out.println("\nAmount of pie = " + pie +(3/4)); } }


Reply With Quote



Bookmarks