Fibonacci Series With Each Iteration

A very basic program asked in written tests in interviews is Fibonacci series.

The series is 0,1,1,2,3,5,8,13,21....

Here in this article I am giving a detailed explanation of the code in each iteration.

The method is,

  1. class TestFibonnaci  
  2. {  
  3.     public void Fibonacci()  
  4.   {  
  5.         int next = 1;  
  6.         int prev = 0;  
  7.         int fib = 0;  
  8.         while (fib < 20)   
  9.         {  
  10.             Response.Write(fib.ToString() + " ");  
  11.             prev = fib;  
  12.             fib = next;  
  13.             next = fib + prev;  
  14.   
  15.         }  
  16.     }  
  17. }  
3 variables

Next=1, prev=0, fib=0

Condition -is fib<20? Yes (0<20)

So Iteration One 
 
Response.Write(fib.ToString()+" "); //Here it prints 0.

prev = fib; the variable prev gets assigned to 0 (i.e; prev=0)

fib = next ; the variable fib gets assigned to 1 (i.e; fib=1)

next = fib + prev; now next=0+1=1
Now }, means 1st iteration completed.


By the end of the first  iteration, values of the variables prev, fib, next are as following,

Prev=0,
Fib=1,
Next=1

Now it checks the condional part.

is fib<20? Yes(1<20) true, so again enters into loop.

Now Second Iteration

Response.Write(fib.ToString()+" "); //Here it prints 1.

prev = fib; the variable prev gets assigned to 1 (i.e; prev=1)

fib = next ; the variable fib gets assigned to 1 (i.e; fib=1)

next = fib + prev; now next=1+1=2
Now }, means 2nd iteration completed.


By the end of the second iteration, values of the variables prev, fib, next are as follows,

Prev=1,
Fib=1,
Next=2

Now it checks condional part
is fib<20? Yes(1<20) true, so again enters into loop.

Now Third Iteration

Response.Write(fib.ToString()+" "); //Here it prints 1.

prev = fib; the variable prev gets assigned to 1 (i.e; prev=1)

fib = next ; the variable fib gets assigned to 2 (i.e; fib=2)

next = fib + prev; now next=2+1=3
Now }, means 3rd iteration completed.


By the end of the third iteration, values of the variables prev, fib, next are as following,

Prev=1,
Fib=2,
Next=3

Now it checks condional part
is fib<20? Yes(2<20) true, so again enters into loop.

Now Fourth Iteration

Response.Write(fib.ToString()+" "); //Here it prints 2.

prev = fib; the variable prev gets assigned to 2 (i.e; prev=2)

fib = next ; the variable fib gets assigned to 3 (i.e; fib=3)

next = fib + prev; now next=3+2=5
Now }, means 4th iteration completed.


By the end of the fourth iteration, values of the variables prev, fib, next are as following,

Prev=2,
Fib=3,
Next=5

Now it checks condional part

is fib<20? Yes(3<20) true, so again enters into loop.

Now Fifth Iteration

Response.Write(fib.ToString()+" "); //Here it prints 3.

prev = fib; the variable prev gets assigned to 3 (i.e; prev=3)

fib = next ; the variable fib gets assigned to 5 (i.e; fib=5)

next = fib + prev; now next=5+3=8
Now }, means 5th iteration completed.


By the end of the fifth iteration, values of the variables prev, fib, next are as following,

Prev=3,
Fib=5,
Next=8

Now it checks condional part
is fib<20? Yes(5<20) true, so again enters into loop.

Now Sixth Iteration

Response.Write(fib.ToString()+" "); //Here it prints 5.

prev = fib; the variable prev gets assigned to 5 (i.e; prev=5)

fib = next ; the variable fib gets assigned to 8 (i.e; fib=8)

next = fib + prev; now next=8+5=13
Now }, means 6th iteration completed.


By the end of 6th iteration, values of the variables prev, fib, next are as following,

Prev=5,
Fib=8,
Next=13

Now it checks condional part
is fib<20? Yes(8<20) true, so again enters into loop.

Now Seventh Iteration

Response.Write(fib.ToString()+" "); //Here it prints 8.

prev = fib; the variable prev gets assigned to 8 (i.e; prev=8)

fib = next ; the variable fib gets assigned to 13 (i.e; fib=13)

next = fib + prev; now next=13+8=21
Now }, means 7th iteration completed.


By the end of 7th iteration, values of the variables prev, fib, next are as following,

Prev=8,
Fib=13,
Next=21

Now it checks condional part.

is fib<20? Yes(13<20) true, so again enters into loop.

Now Eighth Iteration

Response.Write(fib.ToString()+" "); //Here it prints 13.

prev = fib; the variable prev gets assigned to 13 (i.e; prev=13)

fib = next ; the variable fib gets assigned to 21 (i.e; fib=21)

next = fib + prev; now next=21+13=34
Now }, means 8th iteration completed.


By the end of 8th iteration, values of the variables prev, fib, next are as following,

Prev=13,
Fib=21,
Next=34

Now it checks the condional part.

is fib<20? No(21<20) Falls, so CONDITION FAILS HERE , So, it does not enter into the loop.

So the final output is 0 1 1 2 3 5 8 13