About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C Program to Print Fibonacci Series using recursion !

Back to Basics : Fibonacci Series

  • In the 12th century, Leonardo Fibonacci discovered a simple numerical series Called Fibonacci Series.
  • Starting with 0 and 1, each new number in the series is simply the sum of the two before it.
  • To watch more about Fibonacci series - It will Explain everything about this series.

C Program to print Fibonacci Series using recursion !!

#include<stdio.h>
    #include<conio.h>

    int size;
    int fibonacci (int prev_number, int number);

    void main()
    {
        static int prev_number=0,number=1;
        clrscr();

        printf("Enter the Size of Series (< 20) : ");
        scanf("%d",&size);

        printf("1 ");
        fibonacci (prev_number,number);

        getch();
    }

    int fibonacci (int prev_number, int number)
    {
    static int i=1;
    int next_num;

    if (i==size)
       return(0);
    else
        {
         next_num=prev_number+number;
         prev_number=number;
         number=next_num;
         printf ("%d ",next_num);
         i++; // increment counter
         fibonacci (prev_number,number); //recursion
        }
    return(0);
    }
Output :
Enter the Size of Series (< 20) : 6

1 1 2 3 5 8 13

No comments: