Skip to content
Advertisement

Java console printing out “infinity” infinite times instead of value?

I am trying to make a program that estimates pi with the Leibniz equation as an attribute to pi day, however instead of printing out the estimated pi value that I wanted the console is printing out “infinity” for infinite times until I terminate the execution. I am very very confused, help needed! code:

JavaScript

I don’t think I divided anything by zero in the entire program! the Leibniz formula: https://en.wikipedia.org/wiki/Leibniz_formula_for_π console screenshot: screenshot link to my repl project: https://repl.it/@HarryPengRHS/Determining-the-value-of-pi-pi-day-attribute

Advertisement

Answer

Your for loop condition is wrong it should be for (int i = 0; i < iterations; i++) you were using for(int i=0;0<iterations;i++){ 0<iterations which is always true and loop goes to infinite.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement