C++ program to display fibonacci sequence up to n has been shown here. Here n is the limit up to which the sequence is to be generated. For example if n = $50$, we get the fibonacci numbers up to $50$ i.e. $0, 1, 1, 2, 3, 5, 8, 13, 21, 34$. The following section covers the iterative approach to find fibonacci sequence. The algorithm, pseudocode of the program have been shown below.
Page content(s):
1. Algorithm to display fibonacci sequence upto n
1. Take the limit n as input.
2. Assign the first two fibonacci numbers to variables a, b i.e. a = 0 and b = 1
3. If n = 0, display a else, display a, b.
4. Check if a + b ≤ n
5. If step 4 is true perform step 6 to 8, else stop the process
6. t = a + b and display t
7. a = b and b = t
8. Go to step 4
2. Pseudocode to display fibonacci sequence upto n
Input : A limit $n$
Output : Fibonacci sequence upto $n$
1. Procedure fibonacciUptoN($n$):
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13. End Procedure
3. C++ Program & output to display fibonacci sequence upto n using iteration
/***************************** alphabetacoder.com C++ program to find fibonacci series up to N using iteration ******************************/ #include <iostream> using namespace std; int main() { // declare variables int n, a, b, t = 0; // take input of the limit cout << "Enter the limit = "; cin >> n; // intialize the first two terms of the sequence a = 0; b = 1; cout << "Fibonacci sequence upto " << n << ": "; // display the first fibonacci if (n == 0) cout << a; // display first two fibonacci else cout << a << " " << b; //now calculate the remaining terms upto n while (a + b <= n) { // calculate next fibonacci t = a + b; //display next fibonacci cout << " " << t; //assign values for next iteration a = b; b = t; } return 0; }
Output
Enter the limit = 55
Fibonacci sequence upto 55: 0 1 1 2 3 5 8 13 21 34 55

This C++ code calculates and prints the Fibonacci sequence up to a given limit using iteration. Here's a breakdown of the code:
 The code starts with some comments that provide information about the purpose of the program.
 The #include
directive is used to include the input/output stream library, which allows the program to perform input and output operations.  The line using namespace std; allows the program to use names from the std namespace without explicitly specifying it.
 The main() function is the entry point of the program.
 Inside the main() function, several variables are declared: n (the limit), a and b (terms of the Fibonacci sequence), and t (temporary variable for calculations).
 The program prompts the user to enter the limit of the Fibonacci sequence by displaying the message "Enter the limit = " and reads the input using cin >> n;.
 The first two terms of the Fibonacci sequence, a and b, are initialized to 0 and 1, respectively.
 The program then displays a message indicating the limit entered by the user: "Fibonacci sequence upto
: ".  The program checks if the limit entered is 0 (n == 0). If so, it prints only the first term of the Fibonacci sequence, which is 0. Otherwise, it prints the first two terms of the sequence, separated by a space.
 The code enters a while loop that continues until the sum of the current terms (a + b) exceeds the limit n.
 Inside the loop, the next Fibonacci term is calculated by adding the current terms: t = a + b.
 The calculated Fibonacci term t is displayed by printing it followed by a space.
 The values of a and b are updated for the next iteration: a = b and b = t.
 Once the loop exits, the main() function returns 0, indicating successful program execution. Overall, the code calculates and prints the Fibonacci sequence up to the specified limit using iteration, starting with the initial terms 0 and 1.
4. C++ Program & output to display fibonacci sequence upto n using recursion
/***************************** alphabetacoder.com C++ program to find fibonacci series upto N using recursion ******************************/ #include <iostream> using namespace std; // recursive function to display // fibonacci sequence void fibonacci(int a, int b, int n) { if (a <= n) { cout << a << " "; fibonacci(b, a + b, n); } } int main() { // declare variables int n; // take input of the limit cout << "Enter the limit = "; cin >> n; // display fibonacci upto n cout << "Fibonacci sequence upto " << n << ": "; // call the function // pass value of the first two // terms and limit fibonacci(0, 1, n); return 0; }
Output
Enter the limit = 10
Fibonacci sequence upto 10: 0 1 1 2 3 5 8
 The #include
directive is used to include the input/output stream library, allowing the program to perform input and output operations.  The line using namespace std; allows the program to use names from the std namespace without explicitly specifying it.
 The fibonacci() function is declared, which is the recursive function responsible for displaying the Fibonacci sequence.
 Inside the fibonacci() function, there are three parameters: a, b, and n. a and b represent the current terms of the Fibonacci sequence, and n is the limit.
 The function checks if the value of a is less than or equal to the limit n. If true, it prints the value of a followed by a space.
 Then, the function calls itself recursively, passing b as the new a value and a + b as the new b value, while keeping n unchanged.
 The recursive calls continue until a exceeds the limit n, at which point the function returns and the control goes back to the previous recursive call.
 The main() function is the entry point of the program.
 Inside the main() function, the limit n is declared as a variable.
 The program prompts the user to enter the limit of the Fibonacci sequence by displaying the message "Enter the limit = " and reads the input using cin >> n;.
 The program displays a message indicating the limit entered by the user: "Fibonacci sequence upto
: " .  The fibonacci function is called, passing the initial values of the Fibonacci sequence (0 and 1) and the limit n as arguments.
 The recursive function fibonacci is responsible for calculating and printing the Fibonacci sequence up to the specified limit.
 Once the fibonacci function returns, the main() function returns 0, indicating successful program execution.