Calculate Fibonacci Series
This is one of the most asked questions in interviews, calculating and printing the Fibonacci series.
Let’s first try the iterative approach that is simple and prints all the Fibonacci series by ing the length.
Please note that we are starting the series from 0 (instead of 1).
public static voidFibonacci_Iterative(int len)
{
int a = 0, b = 1, c = 0;
Console.Write("{0} {1}", a,b);
for (int i = 2; i < len; i++)
{
c= a + b;
Console.Write(" {0}", c);
a= b;
b= c;
}
}
Output

We can also use the recursive way to print the series against the length as below.
public static voidFibonacci_Recursive(int len)
{
Fibonacci_Rec_Temp(0, 1, 1, len);
}
private static voidFibonacci_Rec_Temp(int a, int b, int counter, int len)
{
if (counter <= len)
{
Console.Write("{0} ", a);
Fibonacci_Rec_Temp(b, a + b, counter+1, len);
}
}
Here we have used two functions just to only the length as an input parameter. In the second method, 4 parameters are required since we need to continue changing the variable's position (in other words, a -> b and b -> a + b).
We also need to increment the counter in each recursion call to compare it with the length and continue the loop until it exceeds the length parameter.
Output

Calculate the nth Fibonacci number
Calculating the nth Fibonacci number is not difficult. We just need to the value with the right index.
We will first have a look at an iterative approach.
Here we are using an integer array to keep the Fibonacci numbers until n and returning the nth Fibonacci number.
public static int GetNthFibonacci_Ite(int n)
{
int number = n - 1; //Need to decrement by 1 since we are starting from 0
int[] Fib = new int[number + 1];
Fib[0]= 0;
Fib[1]= 1;
for (int i = 2; i <= number;i++)
{
Fib[i] = Fib[i - 2] + Fib[i - 1];
}
return Fib[number];
}
Output

We can also look for a recursive version of that. The only thing to consider is here. We need a number less than 1 to get the nth Fibonacci number.
For example, if we want the 8th Fibonacci number, then we need 8-1 (in other words, 7) as an input (we can also create a -through method alternatively).
public static int GetNthFibonacci_Rec(int n)
{
if ((n == 0) || (n == 1))
{
return n;
}
else
returnGetNthFibonacci_Rec(n - 1) + GetNthFibonacci_Rec(n - 2);
}
Output

Please inform me of your comments/suggestions or if you have any better way to do it.

Sarita PrasadPosted Aug 13, 2023, 7:07 AM
Nice explanation
Prakash TripathiPosted Mar 10, 2016, 4:03 AM
Thnx haliza talib.
haliza talibPosted Mar 10, 2016, 2:53 AM
good job
Prakash TripathiPosted Mar 6, 2016, 9:53 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 9:53 PM
Thnx Karthiga.
Prakash TripathiPosted Mar 6, 2016, 9:53 PM
Thnx Swathi.
Prakash TripathiPosted Mar 6, 2016, 9:52 PM
Thnx Rishabh.
Prakash TripathiPosted Mar 6, 2016, 9:52 PM
Thnx vijaykumar.
Prakash TripathiPosted Mar 6, 2016, 9:51 PM
Thnx sivapratap.
Prakash TripathiPosted Mar 6, 2016, 9:50 PM
Thnx Saineshwar.
Kumaresh RajalingamPosted Mar 6, 2016, 8:23 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:23 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:12 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:12 PM
Nice explanation
Prakash TripathiPosted Feb 25, 2016, 7:26 AM
Thnx Sonu.
Sonu ChaudharyPosted Feb 25, 2016, 7:16 AM
nice article
Swathi VullangiPosted Nov 22, 2015, 10:13 PM
nice
Rishabh PanditPosted Sep 29, 2015, 3:33 PM
Good program and help hull
Vijaykumar KandimallaPosted Feb 12, 2015, 6:57 AM
nice program for freshers
sivapratap KurapatiPosted Mar 21, 2014, 6:03 AM
thanks!Intresting Article
Saineshwar BageriPosted Feb 24, 2014, 11:45 PM
Nice one