Help with C++ Homework with Fibonacci numbers

Write a program that displays the first 40 Fibonacci numbers. A Fibonacci number is created
by add the previous two, with the first two always being 0 and 1. A partial sequence is as
follows: 0, 1, 1, 2, 3, 5, 8, 13, 21,…. Your table must display 6 numbers per row and use a
spacing of 10 for each number. Don’t forget to include the header file “iomanip” at the top and
use “setw()”. You will need to turn in an algorithm, source code and output.


here is what i have but i get errors!

#include <iostream>

using namespace std;

{
int FirstNumber = 0;
int SecondNumber = 1;
int NumberOfNumbers = 2;
int NewNumber;

// print out the first two numbers
cout << setw(10) << FirstNumber << setw(10) << SecondNumber;
int col = 2;

for (int row = 0; row < (40 / 6 + 1); row++) {
for (; col < 6; col++) {
// accumulate number count
NumberOfNumbers++;
// count the new number
NewNumber = FirstNumber + SecondNumber;
// display the number
cout << setw(10) << NewNumber;
// quit the loop when 40 numbers reached
if (NumberOfNumbers >= 40)
// shift old number out
FirstNumber = SecondNumber;
// shift the new number in
SecondNumber = NewNumber;
}
col = 0;
cout << endl;
}
}
Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/

Don’t forget to include the header file “iomanip” at the top and
use “setw()”.

as the problem description says you need to do this. Also int main() is important.
Last edited on
You seem to be making your code far too complex
Here: Check out my first Fibonacci sequence, Please DO NOT COPY it and hand it in. I am giving this to you as a guide to find ways to simplify your code. Use the certain parts of my code to help you find out what to do better next time :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
 
using namespace std;
 
int main()
{
   int n, c, first = 0, second = 1;
   unsigned long long 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);
}
i do not copy but thanks for reminder! i use what i am told to better understand what i am doing wrong. i sometimes think to much on a matter.
Topic archived. No new replies allowed.