# Thread: Help with simple program

1. ## Help with simple program

Can someone please check if this program is correct.... i'm trying to write a PI approximation....but i can't get the output that i have to obtain....... suggestions?????

Code:
```#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double pi=0, y=1;
int num, i;
char ch;

pi = 4 - 4/3 + 4/5 - 4/ 7 + 4/ 9 - 4/11 + 4/13 - 4/15;

pi = 0;
y = 1;

do
{
cout<<"How many terms do you want to use?\n";
cin>>num;

for (i = 1; i<=num; i++)

if (i%2 == 0)

pi = pi - 4/y;

else
pi = pi + 4/y;
cout<<pi,;

y = y + 2;

cout<<"Do you want another approximation? please enter Y or N\n";
cin>>ch;

}while (ch!= 'n' && ch!= 'N');

return 0;

}```

2. should put you in right direction

Code:
```#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double pi, y=1;
int num, i;
char ch;
pi = 4.0 - 4.0/3.0 + 4.0/5.0 - 4.0/7.0 + 4.0/9.0 - 4.0/11.0 + 4.0/13.0 - 4.0/15.0;
y = 17.0;

do
{
cout<<"How many terms do you want to use?\n";
cin>>num;

for (i = 1; i<=num; i++)
{
if (i%2 == 0)

pi = pi - 4/y;

else
pi = pi + 4/y;
y = y + 2;
}
cout<<pi;
cout<<"Do you want another approximation? please enter Y or N\n";
cin>>ch;

}while (ch!= 'n' && ch!= 'N');

return 0;

}```

3. You have to watch your whitespace. You only need to print the number after you are out of the loop. The number of terms needs to be very high for accurate results.

Code:
```	do
{
cout<<"How many terms do you want to use?\n";
cin>>num;

for (i = 1; i<=num; i++)
{
if (i%2 == 0)
{
pi = pi - 4/y;
}

else
{
pi = pi + 4/y;
}
y = y + 2;
}

cout<<pi<<endl;

cout<<"Do you want another approximation? please enter Y or N\n";
cin>>ch;

pi = 0;

} while (ch!= 'n' && ch!= 'N');```

4. thank you so much for your help.....i can see now where i made my mistakes....

thanks!!!!!!

5. You also set pi back to 0 after assigning it.

Code:
```      pi = 4 - 4/3 + 4/5 - 4/ 7 + 4/ 9 - 4/11 + 4/13 - 4/15;

pi = 0;
y = 1;```

6. Originally Posted by Tonto
You have to watch your whitespace.
IMAO, so do you. TABs indent too far. Use 3-4 spaces instead