Header Ads

Header ADS

Solution of URI 1176::Fibonacci Array

Before seeing the solution make sure that you have tried enough. Don’t copy the whole code, just find out the logic. If you feel any problemyou will inform me by comment.




#include <bits/stdc++.h>
using namespace std;

int main(){
    int T,N;
    long long int fp,sp,position;
    cin>>T;

    while(T--){
        cin>>N;
        fp=0,sp=1;
        if(N==0){
            cout<<"Fib(0)"<<" = 0"<<endl;
        }
        else if(N==1){
            cout<<"Fib(1)"<<" = 1"<<endl;

        }
        else {
        for(int i=2;i<=N;i++){
            position = fp + sp;
            fp = sp;
            sp = position;

        }
        cout<<"Fib("<<N<<") = "<<position<<endl;
    }

   }
    return 0;
}

No comments

Theme images by konradlew. Powered by Blogger.