Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

HELP MY FIRST PROGRAM !!!!!!

LC3PROLC3PRO Member Posts: 16
Write a program in C that will generate the Fibonacci sequence of numbers. The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21,... or the next number is the sum of the previous two numbers. Generate 24 of the numbers in the sequence, starting with 0, and put each number on a separate line
plz help me!!!!!!!!!!!

Comments

  • connectconnect Member Posts: 1
    Although your problem can be solved using an array from 24
    elements, the following code seems to me more rational. Here,
    function fibonacci() is recursive, that is, it calls itself.

    #include "stdio.h"
    #include "conio.h"

    int fibonacci(int up_to);

    int main(void)
    {
    register int i;

    for(i=1;i<=24;i++)
    printf("%d
    ",fibonacci(i));
    getch();
    return 0;
    }

    int fibonacci(int up_to)
    {
    if(up_to==1) return 0;
    else if(up_to==2) return 1;
    else return fibonacci(up_to-1)+fibonacci(up_to-2); //recursion
    }
  • LC3PROLC3PRO Member Posts: 16
    Thanks, do you know any free C compiler so I can check these ?
  • craniumcranium Member Posts: 6
    [b][red]This message was edited by cranium at 2004-11-12 12:11:38[/red][/b][hr]
    this is my solition without using array.....of course maybe better to use array...it s up to us:-)play play and play around,and im new about programming too...

    //decleration
    int x, s, counter;
    int s1;
    //initi
    x=0; //prev s
    s=1; //new sum
    s1=1; //prev sum
    counter=0; //my counter


    //use while loop
    while (counter<=20)
    {
    print((s1)+" ");
    s1=s+x;
    x =s;
    s=s1;


    counter=counter+1; //counter increment


    }
    //end while

    //wait...



Sign In or Register to comment.