[ c++] fibonacci series By me

This is a discussion on [ c++] fibonacci series By me within the C++ Programming forums, part of the General Programming Boards category; Fibonacci number on Wiki http://en.wikipedia.org/wiki/Fibonacci_number Code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; int power(int a, int b) ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Red face [ c++] fibonacci series By me

    Fibonacci number on Wiki

    http://en.wikipedia.org/wiki/Fibonacci_number

    Name:  77030_10151402847947845_471186108_n.jpg
Views: 453
Size:  60.6 KB

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    int power(int a, int b)
    {
         int c=a;
         for (int n=b; n>1; n--) c*=a;
         return c;
    }
    
    double power(double a, int b)
    {
          float c=a;
          for (int n=b; n>1; n--) c*=a;
          return c;
    }
    
    int main()
    {
    
    double first,c1,c2,r1,r2,r3; int r,l;
    
        first=0.4472135955;  c1=1.618033989; c2=-0.6180339887;
    
    cout<< " Enter  long The series ";
    cin>>l;
    
            for (int i=1; i<l; i++)
    
            {
               r1=power(c1,i);
               r2=power(c2,i);
               r3= r1-r2;
               r= first*r3;
               cout<<r<<",";
            }
            cout <<endl;
        system("pause");
        return 0;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    You forgot to ask a question, such as why the results you're getting are incorrect.
    Using a float inside your double version of power would be one mistake.
    Also, seeing as how you're limiting yourself to the range of ints anyway, you aren't gaining anything by using the approximation method.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop for fibonacci series
    By jackson6612 in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2011, 01:09 PM
  2. c++ Program to find the sum of the Fibonacci Series
    By newbie09 in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2011, 11:55 PM
  3. Fractions in fibonacci series
    By Jamkirk in forum C Programming
    Replies: 4
    Last Post: 01-10-2008, 12:01 PM
  4. Fibonacci series using recursion
    By IPCHU in forum C Programming
    Replies: 1
    Last Post: 12-07-2006, 05:05 AM
  5. Fibonacci series using a recursive function
    By Dargoth in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2002, 11:54 PM

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