Hey, I'm not new to C++ I just can't write this recursively lol. It's easy using iteration but I just don't know how using recursive techniques. Could you plz help! :)
Write a program that when given a positive integer i, uses recursion to compute the "i"th entry of the Fibonacci sequence. The Fibonacci sequence is defined by
f0 = 0
f1 = 1
and
fi = fi-1 + fi-2, for i > 1
0,1,1,2,3,5,8 etc...
Copyright © 2025 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
int function fibonacci( int x){
if (x==0{
return 0;
}
else if(x==1){
return 1;
}
else{
return fibonacci(x-1)+fibonacci(x-2)
}
}
for example for x=2 it will return:
fibonacci(1)+fibonacci(0) = 1+ 0 = 1
for example for x=3 it will return:
fibonacci(2)+fibonacci(1) = fibonacci(1)+fibonacci(0) + fibonacci(1) = 1 + 0 + 1 = 2
and so on...
EDIT: the second IF should have been (x==1), i just fixed it
Also, please note that there might be a syntax error at the function declaration, because it's been many years since i last used c++. but I assure you the logic is correct. so if you have c++ experience, you PERHAPS need to adjust the syntax.