This program runs very smoothly, except that it doesn't print the value of i and is obviously not running the second loop to calculate the margin of error but I don't understand why. Any help?

// This program was designed to calculate the value of pi
import java.util.Scanner;
public class Leibniz {
	public static void main(String[] args)
	{
		Scanner input= new Scanner(System.in);
		System.out.println("Please enter the number of terms to sum");
		
		//Asks user to input range of numbers
		
		int terms= input.nextInt();
		
		int nextValue = 1;
		int Counter = 0;
		double pi1 = 0.0; 
		int operation= 1;
		
		// The loop to sum and calculate pi
		
		while(Counter < terms)
		{
		     pi1 += operation* 4.0/ nextValue; 
		     operation*= -1; //to contrast the minus and plus in the method
		     nextValue += 2; // to keep the denominator an odd number
		     Counter++;
		}
		
		System.out.println(pi1);
		
		double pi= 3.141592653589793;
		System.out.println(pi);
		
		System.out.println("Please enter the value of error");
		
		//Asks user to input the error value
	
		double error= input.nextDouble();
		
		int i=0;
		while (pi1!=pi){
			if (pi1>pi){
				 pi1-=error;
				 
			}else if (pi1<pi){
			     pi1+=error;
				
			}i++;
		}
		   System.out.println(i);
	}

}

Thank you!

I'm confused a bit... Could you actually give a sample input that you did? And what the value displayed? To me, the "terms" looks fine, but why do you need to input the value of error? Is the value something like + or - 0.000001??? Because if it is, you aren't going to get it correctly but goes into an infinite loop in your last while-loop. Why? I will give you an example...

Let say you get your pi from number of x terms as 3.1415931 (pi1) and you enter error as 0.0001.

loop #1: pi1>pi? Yes => pi1 -= 0.0001 => pi1 is now 3.1414931
loop #2: pi1>pi? No => pi1<pi? Yes => pi1 += 0.0001 => pi1 is now 3.1415931 (back to original)
loop #3: repeat as loop #1

See? pi1 will never be equal to pi and will never get out of the loop...

I have a feeling that you are interpreting the "error" meaning incorrectly... Just my feeling...

I see what you mean. The problem is the assignment says that the error can be either 0.1 or 0.01 or 0.001 which means that the error is not a constant value. Isn't there a better way to make that work? Also the loop is not giving any value, the program just goes quiet after I enter the value of the error.
I might need to ask the teacher tomorrow if this is what he meant by margin error because it was very vague.

If I understand what your instructor wants, it means that you need to compute your pi more times if the margin of error is beyond the value that a user specified. What you need to do is to either keep computing PI value or stop. The condition check is the absolute value of real PI value subtract with the PI value you computed. That's the margin of error.

double pi1 = 0.0; 
...  // you computed your pi1 value here
double pi= 3.141592653589793;
...
double error= input.nextDouble();
int i=0;
while (Math.abs(pi-pi1)>error) {
  ... // recompute pi1 using the same way you did above, but this time
  ... // you do it one at a time. Remember to keep all values of denominator!
}

That method really worked but when I asked the teacher he re-explained it to me and it turned out he wants something like this:

// This program was designed to calculate the value of pi

import java.util.Scanner;

public class Leibniz {
	public static void main(String[] args)
	{
		Scanner input= new Scanner(System.in);
		
		int nextValue = 1;
		double pi1 = 0.0; 
		int operation= 1;
		
			
		
		double pi= 3.141592653589793;
		System.out.println(pi);
		
		System.out.println("Please enter the value of error");
		
		//Asks user to input the error value
	
		double error= input.nextDouble();
		int i=0;
		
		 do{
			 pi1 += operation* 4.0/ nextValue; 
		     operation*= -1; //to contrast the minus and plus in the method
		     nextValue += 2; // to keep the denominator an odd number
		     i++;
		} while ((pi1>=pi-error)&&(pi1<=pi+error));
		 
		System.out.println(i);
}}

I was thinking of using 'case' instead for the three (0.1, 0.01, 0.001) errors but case doesn't accept double as a condition value. :(
I am totally confused!

Hmm... Your "while" condition and mine are very similar if an only if mine ">" is changed to ">="??? The only different is that mine won't compute if it is already in between the margin of error (use while instead of do-while). I will show you why...

condition ((pi1>=pi-error)&&(pi1<=pi+error)) This mean...

pi-error <= pi1 <= pi+error
-error <= (pi1 - pi) <= error

Once you apply Math.abs(), the negative value disappears and becomes...
Math.abs(pi1-pi) >= error
==> which is the same as
Math.abs(pi-pi1) >= error

Hope this help to clear up your confusion.

It clarified very much, thank you! I still don't understand though why it works with your condition with the abs but doesn't with mine, even though they both mean the same thing.
But, it's working now so thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.