#include
using namespace std;
int fib(int n)
{
if(n==1)
{
return 2;
}
else if(n==2)
{
return 3;
}
else
{
return fib(n-1)+fib(n-2);
}
}
int main() {
int t;
cin>>t;
while(t>0)
{
int n;
cin>>n;
cout<<"#"<<t<<" “<<”:"<<" "<<fib(n)<<endl;
t–;
}
return 0;
}
what is wrong in my code?
Class assignment recursion
Hi
Fib series is
0 1 1 2 3 …
Now it is in order
0 1 2 3 4 … Fibonacci number
So when n==0 return 0 and n==1 return 1
#include using namespace std; int fib(int n) { if(n==0) { return 0; } else if(n==1) { return 1; } else if(n==2) { return 3; } else { return fib(n-1)+fib(n-2); } } int main() { int t; cin>>t; while(t>0) { int n; cin>>n; cout<<"#"<<t<<" “<<”:"<<" "<<fib(n)<<endl; t–; } return 0; }
still not working
hi ajay
your first code is correct
i think its of fibonacci
your error is
int the cout statement when u r printing the t
for eg
u takr input
t=5
n=1,2,3,4,5
your output come
#5 : 2
#4 : 3
#3 : 5
#2 : 8
#1 : 13
but output should be
#1 : 2
#2 : 3
#3 : 5
#4 : 8
#5 : 13
beacuse ur running t form t to 1
but u have to run the t from 1 to t
hope u get this