**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 the Fibonacci series program in c in two ways as:

**Using for Loop****Using While Loop****By using recursion**

## Using for Loop

This is the first method to print the Fibonacci series.

In this, we use the for loop to iterate number to range up to which we want to print the Fibonacci series.

## Write a program to print the Fibonacci series using for loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#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 to range up to which we want to print the Fibonacci series.

## Write a program to print the Fibonacci series using a while loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#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 the third method to print the Fibonacci series.

In this, we recursively call the function calculating the series to range up to which we want to print the Fibonacci series.

## Write a program to print the Fibonacci series using recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#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 function printf(“\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 is Fibonacci series logic.