forked from Pradeepsingh61/DSA_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fibonacci sequence.cpp
65 lines (54 loc) · 2.3 KB
/
Fibonacci sequence.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream> // Include for input and output streams
#include <vector> // Include for using vectors
using namespace std; // Use the standard namespace to avoid prefixing with std::
/*
* Explanation of Fibonacci Sequence:
* The Fibonacci sequence is a series of numbers where each number
* (after the first two) is the sum of the two preceding ones.
* It typically starts with 0 and 1.
* Example: The first 10 Fibonacci numbers are:
* 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
*/
/*
* Function to generate Fibonacci sequence using the iterative approach.
* Takes an integer n as input and returns a vector containing the first n Fibonacci numbers.
*/
vector<int> fibonacci_iterative(int n) {
vector<int> fib_sequence; // Vector to store Fibonacci numbers
// Return empty vector for non-positive input
if (n <= 0) return fib_sequence;
// If n is 1, return vector with the first Fibonacci number
else if (n == 1) {
fib_sequence.push_back(0);
return fib_sequence;
}
// If n is 2, return vector with the first two Fibonacci numbers
else if (n == 2) {
fib_sequence.push_back(0);
fib_sequence.push_back(1);
return fib_sequence;
}
fib_sequence.push_back(0); // First Fibonacci number
fib_sequence.push_back(1); // Second Fibonacci number
// Loop to calculate the rest of the Fibonacci numbers
for (int i = 2; i < n; ++i) {
// Calculate the next Fibonacci number as the sum of the last two
int next_fib = fib_sequence[i - 1] + fib_sequence[i - 2];
fib_sequence.push_back(next_fib); // Add the next Fibonacci number to the sequence
}
return fib_sequence; // Return the complete Fibonacci sequence
}
int main() {
int n; // Variable to hold the number of Fibonacci numbers to generate
cout << "Enter the number of Fibonacci numbers to generate: ";
cin >> n; // Input from the user
// Generate the Fibonacci sequence
vector<int> fib_sequence = fibonacci_iterative(n);
// Output the generated Fibonacci sequence
cout << "The first " << n << " Fibonacci numbers are: ";
for (int num : fib_sequence) {
cout << num << " "; // Print each number in the sequence
}
cout << endl; // Print a newline after output
return 0; // End of the program
}