Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem with a loop

$
0
0
I had to make a program calculating Pi, n number of times with the formula Pi = 4[1-1/3+1/5-1/7+1/9…+(((〖-1〗^n )))/((2n+1) )] . The user is supposed to enter n and run until the user tells it to stop, which I did. But the user can choose to run the program again with a new value of n and calculate a new value of Pi. The problem is that my program keeps adding the last value of Pi to the new value of Pi. What am I doing wrong? I can't figure out what is going wrong.

I am new at this, so if it is really obvious dont make fun of me :P/>/>

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

int main ()
{
	double n;
	double top, bottom,x, total=0;
	char option;
do
{
	cout << " Enter the number of terms for the program to calculate: ";
	cin >> n;


	for ( double count=0; count <= n ;  count++)
	{
		top = pow(-1,count);
		bottom = (2*count) + 1;
		x= 4*(top/bottom);

		total +=x;
	}

cout << " Pi = " << total;

cout << "\nRun again? (Y/N) ";
cin >> option;

} while (option == 'Y' || option == 'y' );

	
	system("pause");
	return 0;
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>