Whats wrong in this program in C++ to print the fibonacci series ?
#include
In c++ you should only append .h for c headers not cpp headers. ``` #include<iostream> ``` main() is of type integer not void. ``` int main() ``` cout is a function in std. ``` std::cout ``` you set both x and y to same value in this step x=z,y=x; this is wrong.
Can you please write a corrected version ? Of the last part ? I'll understand better then :)
how does the << operator work?
That is a default for output
oh, so it outputs z each iteration?
Yup
if the y=x happens after the x = z, then y and x end up holding the same term number, instead of the highest 2 term numbers - then adding up x and y next iteration wouldn't give you the next term in the fibonacci sequence, just would equal 2*z.
Oh Oh ! So if I write y=x first and then x=z ? Will that be okay ?
should be in terms of algorithm :) though idk c++ syntax
Let me compile it that way.
IT WORKED
Thank you so much both of you :)
yw! does it make sense?
Yep it does :) Now that you have explained it, yes it does :)
ya, say you are holding y = 5 and x = 8, when you do z = x + z; z = 13, the next term in the sequence-- now you need x and y to reset to the term 13, and 8, so that when you add them next time, it will = the next term in the sequence. y = x will give y the term before 13 & x = z will give x the new highest term value of 13, but doing x = z first, will indeed set it equal to 13, but when you do y = x, you also get 13, and the addition of x & y next iteration will just be 13 * 2 ik I already said this, but just thought this looks much neater w/ examples & sitch.
And looks like Lyrae has some helpful tips for proper syntax ^^
Even if the program runs without making those changes, I'm sure there is good reason for them.
Thanks a lot :) @Lyrae
Join our real-time social learning platform and learn together with your friends!