1 int f (int n) {
2 return (n < 3) ? n-1 : f(n-1) + f(n-2);
}
Answers
Answered by
1
Answer:
3 will be the right ans for the above question
Answered by
4
Answer:
If n<3 , the function will return the value of n-1.
Explanation:
- Let the value of n be 2. In this case the function will return n-1 i,e. 2.
- let the value of n be 4. (n>3) is false so f(n-1)+f(n-2) will execute.
f(n-1) is f(3), when f(3) will be called it will call f(2) and f(1).
so we can project the scenario like this:
- f(4) will return f(3)+f(2)
- f(3) will return f(2)+ f(1)
- f(2) will return the value 1
- f(1) will return the value 0
- so we can write f(4)=f(3)+f(2)=f(2)+f(1)+f(2)=2f(2)+f(1)=2.1 +0=2+0=2
Hence return statement will return 2 if n=4.
Similar questions