Fibonacci got different outputs
I tried to find the nth fibonacci number. But if i enter 0(zero) as input why does it give 0 as output for the for loop and 1 for the recursion? The code: #include<bits/stdc++.h> using namespace std; int f(int n) { if(n <= 1) return n; int last = f(n-1); /// THIS WILL BE EXECUTED FIRST // f(1) may be called many times int slast = f(n-2);/// THEN THIS WILL BE EXECUTED return last + slast; } int main() { cout << "Multiple recursion calls" << endl; int n; cin >> n; int fibo[n]; fibo[0] = 0; fibo[1] = 1; for(int i = 2; i < n; i++){ fibo[i] = fibo[i-1] + fibo[i-2]; } cout << "The nth fibonacci number is " << fibo[n-1] << " or " << f(n)<< endl; return 0; }