A Program to Display Fibonacci Series

In this example, we are giving a program to display fibonacci series up to n number of terms. Fibonacci is a series where a term is the sum of previous two terms. The 1st two terms of the Fibonacci series is 0 followed by 1.


Program to display fibonacci series:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int i, n, term1 = 0, term2 = 1, next_term;
  6. cout << "Enter the number of terms: ";
  7. cin >> n;
  8. cout << "Fibonacci Series: ";
  9. for (i = 1; i <= n; ++i)
  10. {
  11. cout << term1 << ", ";
  12. next_term = term1 + term2;
  13. term1 = term2;
  14. term2 = next_term;
  15. }
  16. return 0;
  17. }

Output:

Enter the no. of terms: 12
			Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89