Like Tree1Likes
  • 1 Post By stahta01

My equation output is incorrect for this homework problem

This is a discussion on My equation output is incorrect for this homework problem within the C++ Programming forums, part of the General Programming Boards category; The value for pi can be determined by the series equation Write an interactive program that asks the user how ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    TEXAS
    Posts
    4

    My equation output is incorrect for this homework problem

    The value for pi can be determined by the series equation
    Write an interactive program that asks the user how many terms of the series equation to use in approximating
    ��. Then calculate and display the approximation. Test your program for 10, 20 and 30.

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main ()
    {
        double series, pi;
        int n, i;
        
        cout <<"Enter the number of terms: \n";
        cin >>i;
        
        for (n=1; n<=i; n++)
        {
            series=((-1)^(n-1))/(2*n-1);
            pi=series+pi;
            cout<<n<<endl;
            cout<<pi<<endl;
            }
        cout <<"The pi in "<<i<<" terms is "<<pi*4 <<endl;
        
        system ("pause");
        return 0;
    }
    My output is wrong I suspect it is from the equation 'series'

    Thank you.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    This symbol does not do what you think "^" it is a bit wise operator.
    Look up the function pow or power from the math library. pow - C++ Reference

    Tim S.
    williamg likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    TEXAS
    Posts
    4
    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework- Morse Code output problem
    By Reboot.Revival in forum C Programming
    Replies: 22
    Last Post: 12-02-2012, 01:00 PM
  2. incorrect output
    By Alint808 in forum C Programming
    Replies: 8
    Last Post: 07-17-2011, 06:21 PM
  3. RPN incorrect output
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 10-14-2010, 12:36 PM
  4. incorrect output
    By linuxman in forum C Programming
    Replies: 4
    Last Post: 01-03-2004, 07:01 AM
  5. Incorrect Output
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-07-2002, 08:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21