Saturday, 30 May 2015

Fibonacci series program in c using recursion


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

void printFibonacci(int);

void main(){

    int k,n;
    long int i=0,j=1,f;
          clrscr();
    printf("Enter the range of the Fibonacci series: ");
    scanf("%d",&n);

    printf("Fibonacci Series: ");
    printf("%d %d ",0,1);
    printFibonacci(n);

    getch();
}

void printFibonacci(int n){

    static long int first=0,second=1,sum;

    if(n>0){
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         printFibonacci(n-1);
    }

}

Sample output:

Enter the range of the Fibonacci series: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89


No comments:

Post a Comment