Thread: My equation output is incorrect for this homework problem

  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
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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, 02: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, 08:01 AM
  5. Incorrect Output
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-07-2002, 09:11 AM