#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    C programming- Fibonacci homework help


    Hey there, I need help with a homework question. I am not sure how to go about this. So far I have the following code, which prints out the Fibonacci sequence, but I need the following info to be displayed separately. I tried using another while loop with the same condition but it did not work, and I cant see how using a for loop or if statement would make sense.

    -Starting with the second number of the sequence, display the division of the number by the
    previous one for the complete sequence (4 decimals precision). Can you see a pattern developing?
    We call this the Golden Number.


    So far I have:where value is the number that is the number I am looking to use for the question I posted above.
    #include <stdio.h>

    int main()
    {
    int firstnumber=1, secondnumber=0, value=1, num=1000000, set=0;


    printf("The Fibonacci Series for values up to 1,000,000 is:\n");

    while(value <= num)
    {

    firstnumber = secondnumber;
    secondnumber = value;
    value = firstnumber + secondnumber;


    set=set+1;
    }
    printf("\nThere are %d values within the fibonacci sequence between 1 and 1,000,000.\n\n", set);


    return 0;
    }
  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Posts
    5,285
    Rep Power
    504
    I don't understand all your requirements but what's the problem? Maybe you're supposed to store the Fibonacci numbers in an array or something. Otherwise, just stick in the division and you're "golden". Remember to divide using floating point numbers instead of integers.
    Code:
    #include <stdio.h>
    
    int main() {
      int firstnumber=1, secondnumber=0, value=1, num=1000000, set=0;
      puts("The Fibonacci Series for values up to 1,000,000 is:\n");
      while(value <= num) {
        if (firstnumber)
          printf("%6d / %6d = %10.8lf\n",secondnumber,firstnumber,((double)secondnumber)/((double)firstnumber));
        firstnumber = secondnumber;
        secondnumber = value;
        value = firstnumber + secondnumber;
        ++set;
      }
      printf("\nThere are %d values within the fibonacci sequence between 1 and %d.\n\n", set,num);
      return 0;
    }
    
    #if 0
    
               1
    phi = 1 + -------------
                     1
                1 + -------
                    1 + ...
     
    phi = 1 + 1/phi
    phi**2 - phi - 1 = 0
    
    #endif
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo