Fibonacci series logic
Fibonacci series is a collection or set of the numbers starting with zero or one, followed by the sum of two preceding numbers.
consider Fibonacci series till N which is implemented as 0,1,(0+1),(0+2)…. N.
Example :
Write Fibonacci series up to 8 numbers.
0,1,1,2,3,5,8,13.
we calculated this series as first two number as 0 and 1 , 0+1 => 1, 1+2=>3 ,3+2=>5,5+3=>8,8+5=>13
We implement Fibonacci series program in c in two ways as:
- Using for Loop
- Using While Loop
- By using recursion
Using for Loop
This is first method to print the Fibonacci series.
In this we use the for loop to iterate number till range up to which we want to print the Fibonacci series.
Write a program to print the Fibonacci series using for loop
Write a program to print the Fibonacci series using for loop
C
123456789101112131415161718192021222324252627282930313233343536373839 | #include<stdio.h>#include<conio.h>int main(){int i,n,first,second,num;printf(“\nEnter the number up to which you want to print series :”);scanf(“%d”,&n);first=0;second=1;printf(“\nThe series is :\n”);printf(“%d\t%d”,first,second);//logic for printing series.for(i=0;i<n;i++){num=first+second;first=second;second=num;printf(“\t%d”,num);}getch();}} |
Output:
Enter the number up to which you want to print series :
5
The series is:
0 1 1 2 3 5 8
Using while Loop
This is second method to print the Fibonacci series.
In this we use the while loop to iterate number till range up to which we want to print the Fibonacci series.
Write a program to print the Fibonacci series using while loop
Write a program to print the Fibonacci series using while loop
C
1234567891011121314151617181920212223242526272829303132333435363738394041 | #include<stdio.h>#include<conio.h>int main(){int i=2,n,first,second,num;printf(“\nEnter the number up to which you want to print series :”);scanf(“%d”,&n);first=0;second=1;printf(“\nThe series is :\n”);printf(“%d\t%d”,first,second);//logic for printing series.while(i<n){num=first+second;first=second;second=num;printf(“\t%d”,num);i++;}getch();}} |
Output:
Enter the number up to which you want to print series :
5
The series is:
0 1 1 2 3 5 8
Using recursion
This is third method to print the Fibonacci series.
In this we recursively call the function calculating the series till range up to which we want to print the Fibonacci series.
Write a program to print the Fibonacci series using recursion
C
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 | #include<stdio.h>#include<conio.h>int fib(int);void main(){int i,n,f;printf(“\nEnter the number up to which you want to print series :”);scanf(“%d”,&n);printf(“\nThe series is :\n”);//logic for printing series.for(i=0;i<n;i++){f=fib(i); //calling recursion functionprintf(“\t%d”,f);}getch();}int fib(int n){int num;if(n==0){return(0);}else{if(n==1){return (1);}else{num=fib(n-1)+fib(n-2);return(num);}}} |
Output:-
Enter the number up to which you want to print series :
5
The series is:
0 1 1 2 3 5 8
This are fibonacci series logic.