Hello World can somebody help me with a c++ question..it's abt Fibonacci :/
Well, you need to put up the qustion or people will not know if they can help you. =)
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
OK, so a basic loop version. What problem are you having with it?
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; }
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).
Thank uuuuu (y)
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.
Yeah it wrks my problm was 'numbers aligned five to a line'.. thanx :)
Ah, I see one other thing. main() is wrong. int main(). Always declare main as an int return type in c++.
Yeah (...it's wrking though..) thanx for ur help..it's highly appreciated..
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. =)
Join our real-time social learning platform and learn together with your friends!