Wednesday, 16 March 2011

Code 8: Technique to generate the Fibonacci series.

Problem Description: 
In mathematics, the Fibonacci numbers are the following sequence of numbers: 0,1,1,2,3,5,8,13,21,34,55,89,... By definition, the first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the previous two. Some sources omit the initial 0, instead beginning the sequence with two 1s.But here we will be considering the series starting with 0.Now to start with the problem we first initialize two variable a=1 and b=0. Now we need to know as how many terms we need in the series and take that as input in a variable (say n).Next we print b having the value 0 as the start of the series.Now using for loop we generate the series by adding a and b; storing this sum in another variable c and then exchanging a with b and b with c ; and then repeating this loop till n-1 (as 0 is already included,we now need to generate only n-1 terms).Printing c every time within the loop generates the desired fibonacci series.



No comments:

Post a Comment