-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from asr0104/master
Create fibonacci.c
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
//Fibonacci Series using Dynamic Programming | ||
#include<stdio.h> | ||
|
||
int fib(int n) | ||
{ | ||
/* Declare an array to store Fibonacci numbers. */ | ||
int f[n+2]; // 1 extra to handle case, n = 0 | ||
int i; | ||
|
||
/* 0th and 1st number of the series are 0 and 1*/ | ||
f[0] = 0; | ||
f[1] = 1; | ||
|
||
for (i = 2; i <= n; i++) | ||
{ | ||
/* Add the previous 2 numbers in the series | ||
and store it */ | ||
f[i] = f[i-1] + f[i-2]; | ||
} | ||
|
||
return f[n]; | ||
} | ||
|
||
int main () | ||
{ | ||
int n = 9; | ||
printf("%d", fib(n)); | ||
getchar(); | ||
return 0; | ||
} |