i have to write a program that displays the value of pi for values for i = 10000,20000.....,and 100000
i have to use the following series

pi = 4(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11_+...+(1/(2i +1))-(1/(2i +1))
any ideas

i have to do a program with looping i cant use arrays

can someone give me a hint and a strategy on how to do this problem

Recommended Answers

That is the equation. Extract out the terms of the equation. I'll give you the first one:
4

Add the next 5 terms to the list, one per line

Jump to Post

What things change from one term to the next?

Why do you keep writing that equation? I'm asking for a list of the terms, not the equation.
Here are the first two terms:
+4/1
-4/3

Jump to Post

Your loop makes no sense.
The equation you posted originally was to sum up 10000's of terms to get the value of pi.

Take it one step at a time.
What is the assignment statement to add a value to variable? That is what you need to do to sum …

Jump to Post

I don't know why you are trying to use two loops. One will do it.

Try having the code in your loop add a single term to the sum each time.
The terms to be added are as I've posted many times:
+4/1
-4/3
+4/5
-4/7
...

If you generalize …

Jump to Post

All 40 Replies

Re: Compute pi 80 80

This your school exercise. Make an effort - describe the processes needed, write some pseudo-code (or real code if you prefer), and we will be happy to comment on it. However, YOU need to make a good faith first effort at solving the problem.

Re: Compute pi 80 80

Look at the terms in the equation. Are there any repeating or stepped values that you can use in a loop?
Write a list of half a dozen terms with one term per line. Look for a pattern

Re: Compute pi 80 80

hmmm ok

public class computingPi {
	public static void main(String args[]){
		double i, add, minus;
		i=0;
		add = (1/(2*i+1));
		for(int a=0;i<=10000 ; i++){
			double ans = 4*((1/(2*i+1)-(1/(2*i+1);
		}
	}

}
Re: Compute pi 80 80

alright i have no idea what am i doing i no i have loop it but i dont think so thats correct

Re: Compute pi 80 80

i see some stuff repeating
i know i have to do a for loop but im horrible and new to this

Re: Compute pi 80 80

What is the pattern you saw when you listed the first half dozen terms in the equation?

You need to figure out the algorithm BEFORE you write the code.

Re: Compute pi 80 80

4((1/(2i +1))-(1/(2i +1)+..........) is this the pattern i is always incrementing by 1

Re: Compute pi 80 80

That is the equation. Extract out the terms of the equation. I'll give you the first one:
4

Add the next 5 terms to the list, one per line

Re: Compute pi 80 80

1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)+(1/17)-(1/19)
so after u plugged 4 which is 1/9 i did 5 6 7 8 9 i extracted the eqn in paper and plugged numbers, yes i see a pattern

Re: Compute pi 80 80

I was trying to get you to put one term per line so you could see the pattern as you went from the value on one line to the next line.
Perhaps you can see it with the terms on one line.
To use a loop you need to see what changes from one term to the next.
Can you see that?

Re: Compute pi 80 80

I was trying to get you to put one term per line so you could see the pattern as you went from the value on one line to the next line.
Perhaps you can see it with the terms on one line.
To use a loop you need to see what changes from one term to the next.
Can you see that?

yes i see the pattern (1/(2(1)-1) - (1/(2(2)-1) + (1/(2(3)-1) - (1/(2(3)-1) + (1/(2(4)-1)

is this the patern you wnat me to look at

Re: Compute pi 80 80

Are you sure those are the first 5 terms? Where is the 4?

What things change from one term to the next?

Re: Compute pi 80 80

Are you sure those are the first 5 terms? Where is the 4?

What things change from one term to the next?

4*(1/(2(1)-1) - 1/(2(2)-1) + 1/(2(3)-1) - 1/(2(3)-1) + 1/(2(4)-1))


sorry forgot to put the four in and arnt these the first 5 i plugged in 1 2 3 4 5

Re: Compute pi 80 80

What things change from one term to the next?

Why do you keep writing that equation? I'm asking for a list of the terms, not the equation.
Here are the first two terms:
+4/1
-4/3

Re: Compute pi 80 80

What things change from one term to the next, starting with the second term?

Why do you keep writing that equation? I'm asking for a list of the terms, not the equation.
Here are the first two terms:
4
-4/3

it adds then subtracts
it adds(i=2) subtracts when(i=3) adds when(i=4) subtracts when (i=5)
i see for a odd number it subtracts for a even number it adds

but yeah its something like x-x+x-x+x-x

Re: Compute pi 80 80

If the terms are:
+4/1
-4/3
+4/5
-4/7
I see that the sign alternates from one term to the next
and the value of the denominator increases by 2 for each term

What is a simple way to change the sign of a number?

Re: Compute pi 80 80

If the terms are:
+4/1
-4/3
+4/5
-4/7
I see that the sign alternates from one term to the next
and the value of the denominator increases by 2 for each term

What is a simple way to change the sign of a number?

multiplying it by -1

Re: Compute pi 80 80

Ok, you've got most of what you need. Now write the expression that adds the next term to the current sum and changes the sign for each term. Put it in a loop and see what you get.

Re: Compute pi 80 80

Ok, you've got most of what you need. Now write the expression that adds the next term to the current sum and changes the sign for each term. Put it in a loop and see what you get.

do {
			ans = 1/(2i-1)*(-1)
			
			
			
		} 
		while (ans<=10000);
Re: Compute pi 80 80

Your code does not sum the terms, it only computes a new one every loop.
How do you change the sign from term to term. This is the tricky bit:
Use a separate variable with the value of 1

What is this for:
while (ans<=10000);

Re: Compute pi 80 80
do {
			ans = 1/(2i-1)*(-1)
                        s = 1;
 
 
		} 
		while (ans<=10000);
                                 ans +s
Re: Compute pi 80 80

Your loop makes no sense.
The equation you posted originally was to sum up 10000's of terms to get the value of pi.

Take it one step at a time.
What is the assignment statement to add a value to variable? That is what you need to do to sum up the terms.

Re: Compute pi 80 80

ok norm this is what I got so far

public class computingPi {
	public static void main(String args[]){
		double var = 0, n,varM = 0, pi;
		n=1;
		pi = 0;
		for (double i =2; i<10000;i++){
			var -= (1.0/(2.0*i-1.0));
			var += (1.0/(2.0*i+1.0));

		}
		pi = 4.0*(1.0 + var);
		System.out.println("answer is " + pi);


	}

}
Re: Compute pi 80 80

What does it print out?

You seem to have reworked the equation so I don't recognize it.
The equation was to sum these terms:
+4/1
-4/3
+4/5
-4/7
...
In the loop you change the denominator by 2 and toggle the sign.

I don't see where you do either of those in your code.

Re: Compute pi 80 80

What does it print out?

You seem to have reworked the equation so I don't recognize it.
The equation was to sum these terms:
+4/1
-4/3
+4/5
-4/7
...
In the loop you change the denominator by 2 and toggle the sign.

I don't see where you do either of those in your code.

i get some number decimal = 2.3242432

Re: Compute pi 80 80

I don't think your code does what the equation does.

Re: Compute pi 80 80

i thought it is a summation thats what i need right and changes signs

Re: Compute pi 80 80

I don't see that your code follows the equation. It does summations but I don't see where it changes the sign every time the denominator changes.
And it does NOT give you the correct answer.

I'd start over.

Re: Compute pi 80 80

how bout the part where it says
var+=
var-=
isnt that sign change

Be a part of the DaniWeb community

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