Sunday, 31 December 2017

C program to print Fibonacci series using recursion

Ques:C program to print fibonacci series of a given range using recursion [such that series last number will be less than or equal to given no.]

#include<stdio.h>
void fib(int ,int ,int );
int main()
{
      int a=1, b=1, n;
      
      printf("Enter the number");
      scanf("%d",&n);

      if(n==1)

          printf("series is %d %d",(a-1),a);

     else

     {
      printf("series is %d %d %d",(a-1),a,b);
      fib(a,b,n);
     }
     return 0;
}

void fib(int a, int b, int n)

{
     int temp;
     
     if((a+b)<=n)
       {
            temp=a;
            a=b;
            b=a+temp;
  
           printf("%d",b);
       
           fib(a,b,n);
       }
}



No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...