Answers for "fibbonaci c++"

C++
0

fibbonaci c++

//dp ;Bottom up approach
#include <iostream>

using namespace std;
int fib(int n)
{
    int fib[n+1];
    fib[0]=0;
    fib[1]=1;
    for(int i=2;i<=n;i++)
    {
        fib[i]=fib[i-1]+fib[i-2];
    }
    return fib[n];
}

int main()
{
    int n;
    cout<<"enter the value whose fibonaaci value you want to claculate:"<<endl;
    cin>>n;
    if(n<=1)
    {
        cout<<"fib is: "<<n<<endl;
    }
    else
    {
        cout<<"fib is: "<<fib(n)<<endl;
    }
    return 0;
}
Posted by: Guest on May-28-2021

Browse Popular Code Answers by Language