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;
}
}
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 :)
#include<iostream>
usingnamespace std;
int main()
{
int n, c, first = 0, second = 1;
unsignedlonglong 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);
}