How can we code this problem with help of recursion?
I have written this code:
#include<bits/stdc++.h>
using namespace std;
int fib(int n)
{
if(n<=2)
{
return n;
}
return fib(n-1) + fib(n-2);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
cout<<fib(n);
}
return 0;
}
But it seems to be wrong.
Please help me with this