Thread: [ c++] fibonacci series By me

  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

    [ c++]  fibonacci series By me-77030_10151402847947845_471186108_n-jpg

    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,318
    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-28-2011, 12:55 AM
  3. Fractions in fibonacci series
    By Jamkirk in forum C Programming
    Replies: 4
    Last Post: 01-10-2008, 01:01 PM
  4. Fibonacci series using recursion
    By IPCHU in forum C Programming
    Replies: 1
    Last Post: 12-07-2006, 06:05 AM
  5. Fibonacci series using a recursive function
    By Dargoth in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2002, 12:54 AM