Mid Coding

Find the nth Fibonacci Number?

int Fibonacci(int n)

if (n <= 1) return n;

int a = 0, b = 1;

for (int i = 2; i <= n; i++)

int temp = a + b;

a = b;

b = temp;

return b;

Explanation:

Iterative DP approach; each Fibonacci number is sum of two previous.

Follow on:

More from C# Programming Tutorial

All questions for this course