C Program to Display Fibonacci Sequence


SUBMITTED BY: kaushal1981

DATE: Feb. 23, 2017, 7:06 a.m.

FORMAT: Text only

SIZE: 802 Bytes

HITS: 1077

  1. The Fibonacci sequence is a series where the next term is the sum of pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1.
  2. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
  3. #include <stdio.h>
  4. int main()
  5. {
  6. int i, n, t1 = 0, t2 = 1, nextTerm = 0;
  7. printf("Enter the number of terms: ");
  8. scanf("%d", &n);
  9. printf("Fibonacci Series: ");
  10. for (i = 1; i <= n; ++i)
  11. {
  12. // Prints the first two terms.
  13. if(i == 1)
  14. {
  15. printf("%d, ", t1);
  16. continue;
  17. }
  18. if(i == 2)
  19. {
  20. printf("%d, ", t2);
  21. continue;
  22. }
  23. nextTerm = t1 + t2;
  24. t1 = t2;
  25. t2 = nextTerm;
  26. printf("%d, ", nextTerm);
  27. }
  28. return 0;
  29. }

comments powered by Disqus