Advertisement
jelyslime

Fibonacci Series generator

Jan 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. void fibonacciSeries()
  2. {
  3. int n, t1 = 0, t2 = 1, nextTerm = 0;
  4.  
  5. cout << "Enter the number of terms: ";
  6. cin >> n;
  7.  
  8. cout << "Fibonacci Series: ";
  9.  
  10. for (int i = 1; i <= n; ++i)
  11. {
  12. // Prints the first two terms.
  13. if(i == 1)
  14. {
  15. cout << " " << t1;
  16. continue;
  17. }
  18. if(i == 2)
  19. {
  20. cout << t2 << " ";
  21. continue;
  22. }
  23. nextTerm = t1 + t2;
  24. t1 = t2;
  25. t2 = nextTerm;
  26.  
  27. cout << nextTerm << " ";
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement