// PROGRAM ID: fibonacci.cpp / Fibonacci Number Sequence Generator // REMARKS: Leonardo of Pisa, who is also called Leonardo Fibonacci, // originated the following sequence of numbers in the year 1202: // 0, 1, 1, 2, 3, 5, 8, 13, 21,... In this sequence, each number is // the sum of the preceding two and is denoted by F // (F for Fibonacci and n for number). n // // Formally, this sequence is defined as // // F = 0 // 1 // // F = 1 // 2 // // F = F + F where n>=1 // n+2 n+1 n // // This program prints the first NUMBER_TO_PRINT Fibonacci numbers. // . 1 . 2 . 3 . 4 . 5 . 6 . 7 . //3456789012345678901234567890123456789012345678901234567890123456789012345 #include using namespace std; const int NUMBER_TO_PRINT=20; // How many Fibonacci numbers to print int main() { int current; // Current Fibonacci number being calculated int firstPrevious; // Previous Fibonacci number in sequence int secondPrevious; // 2nd Previous Fibonacci number in sequence int counter; // Count of how many numbers printed // Initialize cout << "Fibonacci Number Sequence \n\n"; secondPrevious = 0; firstPrevious = 1; cout << secondPrevious << endl << firstPrevious << endl; counter = 2; // Generate and print remaining Fibonacci numbers while (counter < NUMBER_TO_PRINT) { current = firstPrevious + secondPrevious; cout << current << endl; counter++; secondPrevious = firstPrevious; firstPrevious = current; } return 0; } // end main