Skip to content

Commit

Permalink
Fibonacci Series
Browse files Browse the repository at this point in the history
  • Loading branch information
RushilKhantwal authored Apr 18, 2020
1 parent 05f51b1 commit af96522
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
25 changes: 25 additions & 0 deletions fibBU.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<bits/stdc++.h>

using namespace std;

int fib(int n)
{
int dp[100]={0};
dp[0]=0;
dp[1]=1;
for(int i=2;i<=n;i++)
{
dp[i]=dp[i-1]+ dp[i-2];
}
return dp[n];
}
int main()
{

int x;
cin>>x;
cout<<endl;
cout<<fib(x);

return 0;
}
29 changes: 29 additions & 0 deletions fibTD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<bits/stdc++.h>

using namespace std;

int fib(int n,int dp[])
{
//Base Case
if(n == 1 || n== 0)
return dp[n] = n;
//LookUp

if(dp[n] != 0)
return dp[n];

//Rec Case

return dp[n] = fib(n-1,dp)+fib(n-2,dp);

}
int main()
{
int dp[100] = {0};

int x;
cin>>x;

cout<<"\n"<<fib(x,dp);
return 0;
}

0 comments on commit af96522

Please sign in to comment.