Thread: Pi Approximation

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Pi Approximation

    Hey,

    =======
    This program uses n terms of the the infinite series:

    pi*pi = 12*(1 - 1/(2*2) + 1/(3*3) - 1/(4*4) + 1/(5*5) - ...
    ==========

    I know that I should use the loop (for) but how can I represent this series to use it with (for) ...The user can enter any number of n terms to approximate Pi.

    HELP

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the red numbers would be your loop counter.
    The blue numbers just make the entire series regular for nice easy code.

    pi*pi = 12*(1/(1*1) - 1/(2*2) + 1/(3*3) - 1/(4*4) + 1/(5*5) - ...

    x = 1
    Then in the loop
    x *= -1;
    will generate the numerator which toggles +1 -1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Post

    I'm not sure exactly what you want here, but something like this would get up to 100 numbers and store them in an array. If zero is entered then it would break the loop.
    Code:
    int n[100];
    int i;
    
    for(i=0; i<100; i++)
    {
        scanf("&#37;i", &n[i]);
        if(! n[i]) break;
    }
    [edit]Also if you are expecting your compiler to interpret you numbers in your equation as floats you might want to add a decimal point. IE:

    pi*pi = 12.0*(1.0/(1.0*1.0) - 1.0/(2.0*2.0) + 1.0/(3.0*3.0) - 1.0/(4.0*4.0) + 1.0/(5.0*5.0)

    Otherwise I think something like 1/(5*5) would come out as 0.
    Last edited by mike_g; 10-07-2007 at 05:26 AM.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    Quote Originally Posted by Salem View Post
    Well the red numbers would be your loop counter.
    The blue numbers just make the entire series regular for nice easy code.

    pi*pi = 12*(1/(1*1) - 1/(2*2) + 1/(3*3) - 1/(4*4) + 1/(5*5) - ...

    x = 1
    Then in the loop
    x *= -1;
    will generate the numerator which toggles +1 -1
    Salem I'm a beginner to C and just started couple of elementry programs....

    I want from the user to enter the number of n (terms) to approximate pi
    let's say :


    Enter n
    Equation.............
    loop.............. I'M LOST HERE !! .......how can I combine n with the equation and the loop
    ...........
    printf.........

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Can you read a number from the user?

    Can you write a for loop which prints from 1 to n ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    yes I can do it

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OK, so enhance it to print the result of say n*n
    Then perhaps the result of 1/(n*n)
    Eventually, you'll get there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pi - Hm, somethign is not right here.
    By MadnessRed in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2008, 01:07 PM
  2. looking for quick method to calculate pi
    By MindlessXD in forum C++ Programming
    Replies: 16
    Last Post: 07-19-2006, 03:59 PM
  3. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:25 AM
  4. Pi and the standard library
    By FOOTOO in forum C Programming
    Replies: 7
    Last Post: 04-15-2005, 11:23 AM
  5. C for PI
    By Lynux-Penguin in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 07:37 PM