Fibonacci Series
Goal:- Print Fibonacci series upto Given no. of elements.
Method-Used:- Property Of Fibonacci Series.(Recursion)
Explanation:- In the Mathematics,Following series of numbers are known as Fibonacci series:-
0 1 1 2 3 5 8 13 21 34 55 89........So on.
Property- First two numbers of fibonacci series are 0,1 and each next element is the sum of two last elements.
An = An-1+An-2
Program:-
(Without Recursion--)
#include<stdio.h>
#include<conio.h>
int main()
{
int n,c,i,a=0,b=1;
printf("\n\tHow many No. you want to Print:-\t");
scanf("%d",&n);
printf("\n\t%d\t%d",a,b);
for(i=0;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}
(Using Recursion-:)
#include<stdio.h>
#include<conio.h>
void fib(int k)
{
static int a=0,b=1,c;
if(k>0)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
fib(k-1);
}
}
int main()
{
int n;
printf("\n\tHow many No. you want to Print:-\t");
scanf("%d",&n);
printf("\n 0 1");
fib(n-2);
getch();
}
Output:-
If you want to share more information about topic discussed above or find anything incorrect Please do comments.
0 comments:
Post a Comment