Write a recursive function to obtains the first 25 numbers of a Fibonacci sequence.
Q. Write a recursive function to obtains the first 25 numbers of a Fibonacci sequence.
Summary:-
In Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of Fibonacci sequence:
1. Without Recursion
2. By Recursion
Summary:-
In Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of Fibonacci sequence:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . .
1. Without Recursion
#include <stdio.h>
int main()
{
int a=0,b=1,temp;
int x =0;
while(x<10)
{
printf("%d ",b);
temp=a+b;
a=b;
b=temp;
x++;
}
return 0;
}
int main()
{
int a=0,b=1,temp;
int x =0;
while(x<10)
{
printf("%d ",b);
temp=a+b;
a=b;
b=temp;
x++;
}
return 0;
}
2. By Recursion
#include <stdio.h>
int a=0,b=1,temp;
int x =0;
int main()
{
if(x<10)
{
printf("%d ",b);
temp=a+b;
a=b;
b=temp;
x++;
main();
}
return 0;
}
int a=0,b=1,temp;
int x =0;
int main()
{
if(x<10)
{
printf("%d ",b);
temp=a+b;
a=b;
b=temp;
x++;
main();
}
return 0;
}
Comments
Post a Comment