Program to illustrate Fibonacci series using recursion
//fibonacci series of a given number using recursion
#include<stdio.h>
int s,x,a,b,r; // global declaration
int fib(int); // function declaration
int main()
{
int i,n,rev;
printf("enter any number:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
s=fib(i); // calling function
printf("%d\t",s);
}
}
int fib (int x) // called function
{
int a,b;
if(x <= 1)
return (x); // base case
else
{
a=fib(x-1);
b=fib(x-2);
return (a+b); // recursive calling
}
}
output:
//fibonacci series of a given number using recursion
#include<stdio.h>
int s,x,a,b,r; // global declaration
int fib(int); // function declaration
int main()
{
int i,n,rev;
printf("enter any number:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
s=fib(i); // calling function
printf("%d\t",s);
}
}
int fib (int x) // called function
{
int a,b;
if(x <= 1)
return (x); // base case
else
{
a=fib(x-1);
b=fib(x-2);
return (a+b); // recursive calling
}
}
output:
No comments:
Post a Comment