• Source
    1. /* C/C++ program for Memoized version for nth Fibonacci number */
    2. #include<stdio.h>
    3. #define NIL -1
    4. #define MAX 100
    5.  
    6. int lookup[MAX];
    7.  
    8. /* Function to initialize NIL values in lookup table */
    9. void _initialize()
    10. {
    11. int i;
    12. for (i = 0; i < MAX; i++)
    13. lookup[i] = NIL;
    14. }
    15.  
    16. /* function for nth Fibonacci number */
    17. int fib(int n)
    18. {
    19. if (lookup[n] == NIL)
    20. {
    21. if (n <= 1)
    22. lookup[n] = n;
    23. else
    24. lookup[n] = fib(n-1) + fib(n-2);
    25. }
    26.  
    27. return lookup[n];
    28. }
    29.  
    30. int main ()
    31. {
    32. int n = 40;
    33. _initialize();
    34. printf("Fibonacci number is %d ", fib(n));
    35. return 0;
    36. }