C++ Program to Display Fibonacci Series up to N

Fibonacci sequence

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.






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. $a \leftarrow 0$

3. $b \leftarrow 1$

4. If $n == 0$:

5. Display $a$

6. Else:

7.Display $a, b$

8. Repeat until $a + b \leq n$:

9. $t \leftarrow a + b$

10.Display $t$

11.$a \leftarrow b$

12.$b \leftarrow t$

13. End Procedure





3. C++ Program & output to display fibonacci sequence upto n using iteration

Code has been copied
/*****************************
      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:
  1. The code starts with some comments that provide information about the purpose of the program.
  2. The #include directive is used to include the input/output stream library, which allows the program to perform input and output operations.
  3. The line using namespace std; allows the program to use names from the std namespace without explicitly specifying it.
  4. The main() function is the entry point of the program.
  5. 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).
  6. 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;.
  7. The first two terms of the Fibonacci sequence, a and b, are initialized to 0 and 1, respectively.
  8. The program then displays a message indicating the limit entered by the user: "Fibonacci sequence upto : ".
  9. 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.
  10. The code enters a while loop that continues until the sum of the current terms (a + b) exceeds the limit n.
  11. Inside the loop, the next Fibonacci term is calculated by adding the current terms: t = a + b.
  12. The calculated Fibonacci term t is displayed by printing it followed by a space.
  13. The values of a and b are updated for the next iteration: a = b and b = t.
  14. Once the loop exits, the main() function returns 0, indicating successful program execution.
  15. 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

Code has been copied
/*****************************
       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



This C++ code calculates and prints the Fibonacci sequence up to a given limit using recursion. Here's a breakdown of the code:
  1. The #include directive is used to include the input/output stream library, allowing the program to perform input and output operations.
  2. The line using namespace std; allows the program to use names from the std namespace without explicitly specifying it.
  3. The fibonacci() function is declared, which is the recursive function responsible for displaying the Fibonacci sequence.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. The main() function is the entry point of the program.
  9. Inside the main() function, the limit n is declared as a variable.
  10. 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;.
  11. The program displays a message indicating the limit entered by the user: "Fibonacci sequence upto : ".
  12. The fibonacci function is called, passing the initial values of the Fibonacci sequence (0 and 1) and the limit n as arguments.
  13. The recursive function fibonacci is responsible for calculating and printing the Fibonacci sequence up to the specified limit.
  14. Once the fibonacci function returns, the main() function returns 0, indicating successful program execution.
Overall, the code calculates and prints the Fibonacci sequence up to the specified limit using recursion, with the initial terms 0 and 1.