1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pi series problem

Discussion in 'C++' started by stigmatas, Nov 22, 2011.

  1. stigmatas

    stigmatas New Member

    Joined:
    Nov 22, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    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 likes this.
  3. stigmatas

    stigmatas New Member

    Joined:
    Nov 22, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    wow thank you so much!!! perfect!
     

Share This Page