Thursday, September 13, 2012

Fibonacci series using Recursive Function C program

Fibonacci series using Recursive Function C program

Program to print the Fibonacci Series up to given term by using recursive Function in C program. Same Logic will be also work in C++ Programs also!

#include<stdio.h>
void fib(int,int,int);

int main()
{
int n=0;
printf("\nEnter the nth term:\n");
scanf("%d",&n);
int prev=0,next=1;
fib(prev,next,n);
printf("\n");
return 0;
}

void fib(int p,int q,int l)
{
if(l==p)
{
 printf("\n Series Printed Successfully...!");
}
else
{
 printf("\t%d",p);
 int temp=p;
 p=q;
 q=temp+q;
 fib(p,q,l);
}
}

0 comments:

Post a Comment