Featured Post

Wednesday, November 2, 2016

C program to print fibonacci sequence using recursive and non recursive functions



/*C program to print Fibonacci sequence using non recursive functions*/

void fibonacci(int);
void main()
{
   int n;
   printf("\n enter your range");
   scanf("%d",&n);
   fibonacci(n);
}
void fibonacci(int m)
{
   int a=0,b=1,c,i=1;
   printf("\n %d \t %d",a,b);
   c=a+b;
   while(i<=n-2)
   {
       printf("\t %d",c);
       a=b;
       b=c;
       c=a+b;
       i++
   }
}

OUTPUT:
                      enter your range
                      10
                      0   1   1   2   3   5   8   13   21   34



/*C program to print Fibonacci sequence using recursive functions*/

int fibonacci(int);
void main()
{
   int n,c;
   printf("\n enter your range");
   scanf("%d",&n);
   for(c=0;c<n;c++)
   {
     ptinf("\t %d",fibonacci(c));
    }
}
void fibonacci(int d)
{
   if(d==0)
   return 0;
   else if(d==1)
   return 1;
   else
   return fibonacci(d-1)+fibonacci(d-2);
 }


OUTPUT:
                      enter your range
                      10
                      0   1   1   2   3   5   8   13   21   34

                                  
                                  nested structures 
  

                                      

c program to print fibonacci sequence using non recursive and recursive functions, factorial of a given number, gcd of a given number, function call, prototype, definition, call by value, call by reference, local parameters, global parameters, tower of hanoi

No comments:

Post a Comment