Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

Hello World can somebody help me with a c++ question..it's abt Fibonacci :/

OpenStudy (e.mccormick):

Well, you need to put up the qustion or people will not know if they can help you. =)

OpenStudy (anonymous):

Fibonacci numbers are generated from the following algorithm: F1=0 F2=1 Fn=Fn-1+Fn-2 for n>2 Using a single loop, write a program to display the first 15numbers such that the numbers are aligned five to a line.. For instance F10 is directly below F5

OpenStudy (e.mccormick):

OK, so a basic loop version. What problem are you having with it?

OpenStudy (anonymous):

I'm new to programming it's not basic for :/ #include<iostream> using namespace std; main() { int n, c, first = 0, second = 1, next; cout << "Enter the number of terms of Fibonacci series you want" << endl; cin >> n; cout << "First " << n << " terms of Fibonacci series are :- " << endl; for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } cout << next << endl; } return 0; }

OpenStudy (e.mccormick):

Basic loop as opposed to recursion or something that can find large numbers. One problem I see is it looks like you are printing a return every line. For the printing 5 to a line part you will use something like if c % 5 = 0 print a return (endl).

OpenStudy (anonymous):

Thank uuuuu (y)

OpenStudy (e.mccormick):

K, now, I did not run the code. So does it do the numbers you need? If so, just change how your `cout << next << endl;` works, and then you have it in one loop. Oh, and ``` above and below code, on a separate line, causes it to highlight and copy out properly. ``` #include<iostream> using namespace std; main() { int n, c, first = 0, second = 1, next; ``` Copy and paste the top of yours from the browswer into a text file. Then do the same with that section. Yours will probably lose all the returns.

OpenStudy (anonymous):

Yeah it wrks my problm was 'numbers aligned five to a line'.. thanx :)

OpenStudy (e.mccormick):

Ah, I see one other thing. main() is wrong. int main(). Always declare main as an int return type in c++.

OpenStudy (anonymous):

Yeah (...it's wrking though..) thanx for ur help..it's highly appreciated..

OpenStudy (e.mccormick):

np. Have fun! The int has to do with how operating system handle exceptions with programs and the fact that not all compilers do a default return type. For properly written and portable code, always use: ``` int main() { bla bla } ``` Unless you have some library or specific environment that is going to handle all that for you. Just a good thing to do with the syntax early on rather than develop a bad habit. =)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!