[243522 views]
A series of numbers in which each number is the sum of the two preceding or previous numbers is called Fibonacci Series.
For example, Fibonacci series upto 7 numbers is 1, 1, 2, 3, 5, 8, 13.
In above example,
first 2 numbers (1, 1) are printed directly as there are no preceding numbers. But after that, i.e the 3rd number (2) is the sum of 1st and 2nd number (1+1=2). Similarly to get 4th number, we add 2nd and 3rd number. (i.e., 1+2=3). You can use this pattern to find fibonacci series upto any number.
Mathematical expression to find Fibonacci number is :
F_{n}=F_{n-1}+F_{n-2}
i.e. To get nth position number, you should add (n-2) and (n-1) position number.
So, if a user Enters 5,then n=5,
Variables a and b are initialized to 0 and 1 respectively. In Step 5, We directly print a and b, So the Output till Step 5 is-
0 1
In Step 6, value of "i" is 2 as the first two numbers (0, 1) are already printed. Now for every iteration of "i", we add a and b, so variable "c" now becomes 1 (i.e. 0+1). Then the value of "c" is printed. So Output now becomes -
0 1 1
In Step 6.3, we move 2nd last value (which is stored in variable "b") to variable "a" and last value (which is stored in variable "c") to b. So Now,
a=1, b=1
In Step 6.4, Value of "i" is incremented by 1(i.e. i=i+1 ) and iteration again continues until value of "i" is less than user entered number "n".
So, For i=3, Again following steps 6.1 to 6.4, Output will be-
0 1 1 2
So, For i=4, Again following steps 6.1 to 6.4, Output will be-
0 1 1 2 3
So, For i=5, Again following steps 6.1 to 6.4, Output will be-
0 1 1 2 3 5
For i=6, condition i less than n becomes false as (6<=5) is false. Hence the Final Output will be -
0 1 1 2 3 5
Liked Article? Please Buy me a Coffee 😭 |
hoiche flowchart? paren na dite ashen kno?