Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Pi series problem (http://www.go4expert.com/forums/pi-series-problem-t27190/)

stigmatas 22Nov2011 05:58

Pi series problem
 
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 22Nov2011 06:26

Re: Pi series problem
 
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;
}


stigmatas 22Nov2011 06:48

Re: Pi series problem
 
wow thank you so much!!! perfect!


All times are GMT +5.5. The time now is 15:02.