Task is to find out the Fibonacci series for a given range . Before coding the program, first find out the logic of the program.

### Logic

First two Fibonacci no are 0 and 1

A series of numbers in which each subsequent number is sum of its two previous numbers is known as Fibonacci series and each numbers are called Fibonacci numbers.

**Algorithm for Fibonacci series**

_{n}= F

_{n-2}

_{ }+ F

_{n-1}

**Example**

### Code 1:

Fibonacci Using 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 | #include<stdio.h> #include<conio.h> int main() { int no, f1 = 0, f2 = 1, temp, cnt; clrscr(); printf("Enter the range\t"); scanf("%d",&no); printf("\n%d no of fibonacci series are..",no); for(cnt = 0; cnt < no; cnt++) { if(cnt < 2) temp = cnt; else { temp = f1 + f2; f1 = f2; f2 = temp; } printf("\n%d",temp); } getch(); return 0; } |

### Description:

Here, first initialize two variable f1 and f2 as first two value of Fibonacci series and use counter and temporary variable for swap and print. Here counter’s work is to count the value that a user gives as the range of output. First two value will print without swap or add, but after that it will add two value and print the sum as well as swap the variable to next.

### Output

Fibonacci series:

### Code 2

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 | #include<stdio.h> #include<conio.h> int fibbo(int n) { if(n==0) return 0; else if( n== 1) return 1; else return (fibbo(n-1)+fibbo(n-2)); } int main() { int no, temp = 0, cnt; clrscr(); printf("Entere the range\t"); scanf("%d",&no); printf("\n%d no of fibo series is\n",no); for(cnt = 0; cnt < no; cnt ++) { printf("\t%d\n",fibbo(temp)); temp++; } getch(); return 0; } |

### Description

Here first declare a function for iteration. It’s work is to add previous two elements. At the time of compilation, when the function is called, at that time the iteration function will execute.

### Output

Fibonacci series using recursion

If you have any query then post as comment we try to solve it out. For more program come back again.