Problem: Fibonacci Series
Write a C++ program that generates the Fibonacci series up to a specified number of terms. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
Your program should take an input from the user specifying the number of terms in the Fibonacci series to generate and output the series.
Example:
Enter the number of terms: 7
Fibonacci Series:
0 1 1 2 3 5 8
Hint: You can use a loop to generate the Fibonacci series by starting with the first two terms (0 and 1) and iteratively calculating the next term by adding the previous two terms.
You can begin by setting up the basic structure of the program and gradually complete it. Let me know if you need further assistance!
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> fibonacciResult;
int number;
int fibonacci1 = 0;
int fibonacci2 = 1;
cout << "Enter the number of terms: ";
cin >> number;
fibonacciResult.push_back(fibonacci1);
fibonacciResult.push_back(fibonacci2);
for (int i = 2; i < number; i++) {
int fibonacci = fibonacci1 + fibonacci2;
fibonacciResult.push_back(fibonacci);
fibonacci1 = fibonacci2;
fibonacci2 = fibonacci;
}
cout << "Fibonacci Series:" << endl;
for (int i = 0; i < number; i++) {
cout << fibonacciResult[i] << " ";
}
cout << endl;
return 0;
}