# approximating the value of pi

Page 1 of 1

## 8 Replies - 1065 Views - Last Post: 04 October 2012 - 07:40 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294337&amp;s=1fc4f7e3a1c26ce199e23d027a97e61b&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nyb17

Reputation: 0
• Posts: 3
• Joined: 04-October 12

# approximating the value of pi

Posted 04 October 2012 - 06:57 PM

```import java.util.Scanner;

public class PiApprox {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

double n, i;
double x = 0;

do {
System.out.println("Please enter the ending value (the greater this is, the more accurate the approximation will be): ");
i = keyboard.nextDouble();

for (n = 0 ; n <= i ; n++ )
x += ((Math.pow(-1,n)) / ((2 * n) + 1));
System.out.println("The approximated value of pi is: " + (4 * x));

System.out.println("Do you want to run this program again? [y/n]: ");
char y = keyboard.next().charAt(0);

if (y == 'n' || y == 'N')
break;
} while (true);

}

}

```

the task indicates the we must:
Prompt the user for an ending value
Approximate the value of pi and display the answer
Ask the user if they want to try again, and do so accordingly.

I've got most of it; however, when i run it again, the formula gets messed up, and instead of an answer along hte lines of 3.14... i get 6(and a bunch of decimals). I cant understand why, so help would be greatly apprecaited. Thanks a bunch

This post has been edited by jon.kiparsky: 04 October 2012 - 07:05 PM

Is This A Good Question/Topic? 0

## Replies To: approximating the value of pi

### #2 jon.kiparsky

• Pancakes!

Reputation: 9161
• Posts: 15,903
• Joined: 19-March 11

## Re: approximating the value of pi

Posted 04 October 2012 - 07:19 PM

This was subtle - I actually had to run it to see the problem

I'm going to throw the output at you:

Please enter the ending value (the greater this is, the more accurate the approximation will be):
100
The approximated value of pi is: 3.1514934010709914
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
1000
The approximated value of pi is: 6.294085055410544
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
200
The approximated value of pi is: 9.440652802593487
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
101
The approximated value of pi is: 12.572441770166934
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
99
The approximated value of pi is: 15.704034673725477
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
100
The approximated value of pi is: 18.855528074796464
Do you want to run this program ? [y/n]:
n

Do you see the problem now?

### #3 cfoley

• Cabbage

Reputation: 2289
• Posts: 4,807
• Joined: 11-December 07

## Re: approximating the value of pi

Posted 04 October 2012 - 07:26 PM

Edit: Actually, let's run with Jon's problem solving approach.

And it's not subtle, it's sloppy coding. This ain't c !

This post has been edited by cfoley: 04 October 2012 - 07:28 PM

### #4 nyb17

Reputation: 0
• Posts: 3
• Joined: 04-October 12

## Re: approximating the value of pi

Posted 04 October 2012 - 07:30 PM

Thank you very much, to both of you. I really appreciate it. I actually got it while reasoning things out, but none the less, i really appreciate the help.

### #5 jon.kiparsky

• Pancakes!

Reputation: 9161
• Posts: 15,903
• Joined: 19-March 11

## Re: approximating the value of pi

Posted 04 October 2012 - 07:33 PM

cfoley, on 04 October 2012 - 09:26 PM, said:

Edit: Actually, let's run with Jon's problem solving approach.

And it's not subtle, it's sloppy coding. This ain't c !

Subtle, sloppy... you say po-tay-to and I say po-tah-to...

### #6 cfoley

• Cabbage

Reputation: 2289
• Posts: 4,807
• Joined: 11-December 07

## Re: approximating the value of pi

Posted 04 October 2012 - 07:34 PM

In that case, I hope you also fixed the two other occurrences of that problem, even though they didn't have obvious side effects.

### #7 nyb17

Reputation: 0
• Posts: 3
• Joined: 04-October 12

## Re: approximating the value of pi

Posted 04 October 2012 - 07:35 PM

And yeah, I agree, it was a stupid mistake on my part. If only i took the time to look things over 4 hours ago. haha, thanks again. As mentioned, I ended up fixing this mistake as I reasoned things out. I am an idiot!

### #8 jon.kiparsky

• Pancakes!

Reputation: 9161
• Posts: 15,903
• Joined: 19-March 11

## Re: approximating the value of pi

Posted 04 October 2012 - 07:36 PM

nyb17, on 04 October 2012 - 09:30 PM, said:

Thank you very much, to both of you. I really appreciate it. I actually got it while reasoning things out, but none the less, i really appreciate the help.

As long as you get it, that's enough. Sometimes the best way to come up with the answer is to ask the question.

### #9 cfoley

• Cabbage

Reputation: 2289
• Posts: 4,807
• Joined: 11-December 07

## Re: approximating the value of pi

Posted 04 October 2012 - 07:40 PM

Po-tay-to, po-tah-to -- all tastes the same. And accents are sexy anyway.

Sloppy scoping causes bugs.

Anyway, I know you know that. The important point here is that accents are sexy.