Pi series problem

stigmatas's Avatar, Join Date: Nov 2011
Newbie Member
hey, I got this code, but my brother needs it in terms of printf,scanf,if,for,while u know, and i uses cin,cot and those stuff... i don't remember how to write in the terms he want.. somebody can help us

this is his hw:

Calculate the value of pie from teh infinite series
pie = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +...
Print a table that shows the value of pie approximated y 1 term of this series,
by two terms, by three terms etc. How many terms of this series do you hve
to use before you first get 3.14? 3.141? 3.1415?

thanks!

Code:
#include <iostream>
using namespace std;

int main()
{
     float pi = 4, n;
     int j = 0, s = -1;
     cout << "n: ";
     cin >> n;

     cout.precision(18);
     for(float i = 3; j < n; i += 2, j++, s *= -1)
     {
          pi += (4.0F / i) * s;
          cout << j + 1 << ". " << pi << endl;
     }

     system("pause");
     return 0;
}
virxen's Avatar, Join Date: Nov 2009
Pro contributor
try this

Code:
#include <stdio.h>

int main(){
     float pi = 4, n;
     int j = 0, s = -1;
     printf("\ngive n:");
     scanf("%f",&n);getchar();
     for(float i = 3; j < n; i += 2, j++, s *= -1){
          pi += (4.0F / i) * s;
          printf("\n%d> %f",(j+1),pi);
     }
     getchar();
     return 0;
}
shabbir like this
stigmatas's Avatar, Join Date: Nov 2011
Newbie Member
wow thank you so much!!! perfect!