#include
#include
//Function Prototype
void fibo(int);
void fiborec(int,int,int);
void main()
{
int i,num1=0,num2=1;
clrscr();
//Accept User Input
printf("Please enter the no. of terms value : ");
scanf("%d",&i);
//To Call Normal Function
printf("\nWithout Recursion\n");
printf("%5d%5d",num1,num2);
fibo(i-3);
//To Call Recursive Function
printf("\n\nUsing Recursion\n");
printf("%5d%5d",num1,num2);
fiborec(num1,num2,i-2);
getch();
}//End of main()
void fibo(int n)
{
int i,previous=1,sum=1,result=0;
for(i=0;i<=n;i++)
{
sum=previous+result;
result=previous;
previous=sum;
printf("%5d",sum);
}//End of For Loop
}//End of fibo()
void fiborec(int previous,int num2,int count)
{
int sum;
sum = previous + num2;
printf("%5d",sum);
count--;
if(count>0)//Exit Condition (Very Important in Recursion)
{
fiborec(num2,sum,count);
}
}//End of fiborec()
/*OUTPUT
--------
Please enter the no. of terms value : 12
Without Recursion
0 1 1 2 3 5 8 13 21 34 55 89
Using Recursion
0 1 1 2 3 5 8 13 21 34 55 89 */
-----------------------------------
-----------------------------------
No comments:
Post a Comment