0

why doesn't my c++ code outputs?

My code: #include <iostream> using namespace std; int fib(int n){ if (n <= 1){ return n; } return fib(n-1) + fib(n-2); } int main(){ for(int x=100; x>0; x--){ //<----- cout<<fib(x)<<endl; } return 0; } in the for loop if the x is 10, it works if it is 100 it doesn't why?

30th Jun 2021, 7:27 AM
Bhavik Mahalle
Bhavik Mahalle - avatar
3 Answers
+ 3
Not sure, but I think your code gets a execution time out .That's why it doesn't show any output. Can you imagine how many times the fib() function is called when you use a loop to get the value of fib from 100 to 1? It takes so much memory power and takes so much time.I think sololearn doesn't give that much time.
30th Jun 2021, 7:39 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 2
i tested then found out that it works fine at 30, then at 31 it breaks while running after that it just doesn't work
30th Jun 2021, 7:44 AM
Bhavik Mahalle
Bhavik Mahalle - avatar
+ 2
Try in a good compiler or ide like cxxdroid(android) or codeblocks(computer).It works there after 30 also.
30th Jun 2021, 8:07 AM
The future is now thanks to science
The future is now thanks to science - avatar