Thread: still cannnot figure this out

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    still cannnot figure this out

    I am trying to get this fibonacci to work but it is giving me the wrong answers. please help

    #include <stdio.h>

    int cal_fibonacci (int d);


    void main(void)
    {
    int b,c;
    printf("Please enter a WHOLE number:\n");
    scanf("%d",&b);
    c=cal_fibonacci(b);
    }

    int cal_fibonacci (int d)
    {
    int f1;
    int f2;
    int fnew, temp;
    int x;

    for(x=1;x<d;++x)
    {
    while (x<3)
    {
    printf("%d" "1",x);
    printf("F1=1");
    printf("F2=1");
    x+=1;
    }
    fnew=f1+f2;
    printf("%d" "%d",x,fnew);
    f1=temp;
    f2=f1;
    f2=fnew;
    }

    }

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Okay, I explained why your program won't work already and even gave you the solution. The Fibonacci sequence is a simple but important one, you can find a Fibonacci number in a great many places in nature. Here's the sequence:

    1 1 2 3 5 8 13 21 34 55 89 etc...

    The next number in the sequence is the sum of the current number and the number before it. So here's your logic structure, you can build your program from this.
    Code:
                    | n <=  1:  1
    Fibonacci(n) =  | n =   2:  1
                    | n >   2:  Fibonacci(n - 1) + Fibonacci(n - 2)
    If you want to see an implemetation of this in C, view my reply to your previous post.
    pointer = NULL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. can't figure out what's with this code
    By silverstein101 in forum C++ Programming
    Replies: 8
    Last Post: 04-16-2008, 12:45 AM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM