Write a C program to find out the Fibonacci series using Loop/Recursion

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.

pascals-triangle-fibonacci
Algorithm for Fibonacci series
Fn = Fn-2 + Fn-1

Example

0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55  …
2 is Fibonacci number since sum of its two previous number i.e. 1 and 1 is 2

Code 1:

Fibonacci  Using Loop

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:

Fibbo Output

Fibonacci Output

Code 2

Fibonacci series using recursion:-

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

Recursion Fibonacci series output

Recursion Fibonacci series output

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

INSERT IMAGE URL HERE

Related posts:

Hi I am Here
You can leave a response, or trackback from your own site.

Leave a Reply

Powered by WordPress | Download Free WordPress Themes | Thanks to Themes Gallery, Premium Free WordPress Themes and Free Premium WordPress Themes