forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci_dp.c
54 lines (44 loc) · 1.06 KB
/
fibonacci_dp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Fibonacci Series using Dynamic Programming
/* Author: Moinak Banerjee(moinak878)
Date : 1 October ,2019
*/
#include <stdio.h>
#include <stdlib.h>
int fib(int n)
{
// Out of Range checking
if (n < 0)
{
printf("\nNo Such term !\n");
exit(0);
}
// declaring array to store fibonacci numbers -- memoization
int *f = (int *)malloc(
(n + 2) * sizeof(int)); // one extra to handle edge case, n = 0
int i;
/* let 0th and 1st number of the series be 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
// Adding the previous 2 terms to make the 3rd term
f[i] = f[i - 1] + f[i - 2];
}
int out = f[n];
free(f);
return out;
}
int main(int argc, char *argv[])
{
int number;
// Asks for the number/position of term in Fibonnacci sequence
if (argc == 2)
number = atoi(argv[1]);
else
{
printf("Enter the value of n(n starts from 0 ): ");
scanf("%d", &number);
}
printf("The nth term is : %d \n", fib(number));
return 0;
}