Wednesday 3 October 2012

Function015

Write a recursive function to obtain the first twenty five numbers of a Fibonacci sequence.
N.B. [ Example : 1  1  2  3  5  8  13  21  34  55.............. ]

#include <stdio.h>
#include <conio.h>
void fibo ( int, int );                                     /* Function Prototype Declaration */
void main ( )
{                               
        int    i, t, old = 0, current = 1, new;
        clrscr ( );
                           
        printf("\n\n\n\t\t\t      FIBONACCI SEQUENCE");
        printf("\n\t\t\t     ____________________");
        printf("\n\n\n\n %d \t %d\t", old, current);
                            
        fibo ( old, current );                          /* Function calling */
        getch ( );
}                     
                                  
void fibo ( int    old, int    current )            /* Function Definition */
{                              
        static int      terms = 2;
        int    new;
                     
        if ( terms < 20 )
        {                                
                  new = old + current;
                  printf ("%d \t", new );
                  terms = terms + 1;
                  fibo ( current, new );
        }                     
        else                 
                  return;
}

Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...