Thread: Fibonacci sequence

  1. #1
    Registered User
    Join Date
    Sep 2001

    Fibonacci sequence

    What am I doing wrong here, any suggestions....

    #include <iostream>
    #include <cmath>
    //using namespace std;

    int main ()
    int num1, num2, num3;
    float ratio, Gold;

    cout << "Enter the number to end the sequence: << num3 ";
    num1 = 1;
    num2 = 1;
    if (num3 < 500)
    num3 = num1 + num2;
    ratio = num1/ num2;
    num1 = num2;
    num2 = num3;
    cout<< num1;
    cout<< "The ratio is: << ratio);
    cout<< num2;
    Gold = sqrt(5) -1 /2;
    cout << "The Golden ratio is: "<< Gold ";

    return 0;


  2. #2
    Registered User
    Join Date
    Sep 2001
    Try fixing the #include statements...

    #include <iostream.h>
    #include <cmath.h>

    <^>( * ; * )<^>

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    <cmath> and <iostream> are the now standard way of saying <math.h> (the c prefix is because it is an ANSI C library) and the old <iostream.h>. No official headers in the new standard include the .h suffix.

    You didn't elaborate on the problem, though I'll venture a guess that ratio is always 0.

    ratio = num1/num2

    num1 and num2 are both ints, thus the result of that division is an integer. Since num2 is always greater than num1, the result will always be 0.

    This can be changed by casting the terms to floats before performing the operation, like this.

    ratio = (float) num1 / (float) num2
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fibonacci sequence
    By cph in forum C Programming
    Replies: 57
    Last Post: 04-30-2009, 07:17 AM
  2. n-th element of the fibonacci sequence
    By me001 in forum C Programming
    Replies: 22
    Last Post: 09-24-2008, 03:27 AM
  3. Fibonacci Sequence
    By Dogmasur in forum C Programming
    Replies: 15
    Last Post: 08-10-2008, 07:55 AM
  4. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  5. Fibonacci sequence output statement
    By chocha7 in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 11:04 PM