Solution: Compute the Fibonacci Number
We'll cover the following...
C
#include <stdio.h>int fib(int n){int i;if ((n == 0) || (n == 1))return n;int a = 0;int b = 1;int tmp;for (i = 2; i <= n; i++) {tmp = b;b = a + b; // Now b contains the i^th Fibonacci numbera = tmp;}return b;}int main(void){int n = 10;printf("Fibonacci number at position %d is %d\n", n, fib(n));return 0;}
Below is the explanation of the solution: